2014-09-28 3 views
1

libGDX (box2d)에서 본문을 삭제/생성하는 데 문제가 있습니다. 치명적인 오류가 발생합니다. 로그 :libGDX에서 본문 삭제 및 생성

http://pastebin.com/fXWXpe8N

나는 충돌 후 몸을 파괴하려고합니다. ~ 90 %의 시체가 오류 및 충돌없이 양호하게 삭제됩니다. 그러나 때때로 그것은 일어난다. 어떻게이 오류를 해결할 수 있습니까? 나는 모른다.

연락처 :

private void bulletGround(Contact contact) 
{ 
    Body bodyA = null; 
    Body bodyB = null; 

    if(contact.getFixtureA() != null && contact.getFixtureA().getUserData() != null && 
      contact.getFixtureA().getUserData().equals(Ground.USERDATA)) 
     bodyA = contact.getFixtureA().getBody(); 

    if(contact.getFixtureB() != null && contact.getFixtureB().getUserData() != null && 
      contact.getFixtureB().getUserData().equals(Bullet.USERDATA)) 
     bodyB = contact.getFixtureB().getBody(); 

    if(bodyA != null && bodyB != null) 
    { 
     if(!world.deletingList.contains(bodyB, true)) { 
      world.deletingList.add(bodyB); 
     } 
    } 
} 

private void bulletEnemy(Contact contact) 
{ 
    Body bodyA = null; 
    Body bodyB = null; 

    if(contact.getFixtureA() != null && contact.getFixtureA().getUserData() != null && 
      contact.getFixtureA().getUserData().equals(Enemy.USERDATA)) 
     bodyA = contact.getFixtureA().getBody(); 

    if(contact.getFixtureB() != null && contact.getFixtureB().getUserData() != null && 
      contact.getFixtureB().getUserData().equals(Bullet.USERDATA)) 
     bodyB = contact.getFixtureB().getBody(); 

    if(bodyA != null && bodyB != null) 
    { 
     if(!world.deletingList.contains(bodyB, true) && !world.deletingList.contains(bodyA, true)) { 
      world.deletingList.add(bodyB); 
      world.deletingList.add(bodyA); 
     } 
    } 
} 

삭제 :

private void deleteObjects() 
{ 
    for (int i = 0; i < gameWorld.deletingList.size; i++) { 

     Body body = gameWorld.deletingList.get(i); 

     if (body != null && body.getFixtureList().size > 0 && !gameWorld.getWorld().isLocked()) 
     { 
      gameWorld.isDeletingTime = true; 

      body.setUserData(null); 
      body.setActive(false); 
      gameWorld.getWorld().destroyBody(body); 
      gameWorld.deletingList.removeIndex(i); 
     } 
    } 
    gameWorld.isDeletingTime = false; 
} 

만들기 : 당신이 코드에서 난 단지 하나의 오류를 참조

render: 
    if(shootButton.isPressed()) 
     { 
      if(framesForShoot/Gdx.graphics.getFramesPerSecond() > Info.shootingSpeed) 
      { 
       bullet = new Bullet(worldGame, renderer.getGun().getBody().getPosition().x - renderer.angle0fGun, 
         renderer.getGun().getBody().getPosition().y, renderer.angle0fGun); 
       renderer.bulletList.add(bullet); 
       framesForShoot = 0; 
      } 
     } 



public class Bullet 
{ 
    public static final String USERDATA = "bullet"; 
    private GameWorld world; 
    private Body body; 
    private Vector2 position; 
    public Texture bullet_texture; 

    public Bullet(GameWorld world, float startX, float startY, float pos) 
    { 
     this.world = world; 
     body = this.world.createBox(BodyDef.BodyType.DynamicBody, 0.5f, 0.5f, 0); 
     body.setTransform(startX, startY, 0); 
     body.getFixtureList().get(0).setUserData(USERDATA); 

     bullet_texture = new Texture(Gdx.files.internal("data/bullet.png")); 
    } 

답변

2

, 나는 그것이 충돌이 발생하는 것이 확실하지 않다, 그러나 어떤 식 으로든 고쳐야합니다.

gameWorld.deletingList.removeIndex(i); 

같은 배열을 반복하면서 배열에서 항목을 제거하면 안됩니다. 이는 항목을 제거 할 때마다 배열이 항목 순서를 변경하고 크기를 줄이므로 예측할 수없는 버그가 발생할 수 있습니다. 해결 방법은 임시 배열 및 루프를 작성하여 원래 항목에서 항목을 제거 할 수있게하는 것입니다.

또한 deleteObjects()으로 전화 하시겠습니까? render(), world.step() 다음에 beginContact() 또는 endContact()이 아닌 번호로 전화해야합니다.