0

So basically i get an error when trying to destroy bodies that are not in screen bounds. Also my one type bodies start to act strange when other type bodies are destroyed ( example bullets starts to move backwards) here are the code samples :

Maingame loop class:

Array<Body> bodies = new Array<Body>(world.getBodyCount());
    world.getBodies(bodies);
    for (Body body : bodies) {
        check = 0;
        if (BodyUtils.bodyIsEnemy(body)){
            update(body);
            check = 1;
        }
        if (BodyUtils.bodyIsBullet(body) && check == 0){
            update1(body);
            check = 0;
        }
    }

private void update(Body body) {
    if (!BodyUtils.bodyInBounds(body)) {
        if (BodyUtils.bodyIsEnemy(body) && !player.isHit()) {
            createEnemy();
        }
        world.destroyBody(body);
    }
}

private void update1(Body body) {
    if (!BodyUtils.bulletInBounds(body))
        world.destroyBody(body);
}

Other class:

public static boolean bodyInBounds(Body body) {
        UserData userData = (UserData) body.getUserData();
        switch (userData.getUserDataType()) {
        case ENEMY:
            return body.getPosition().x + userData.getWidth() / 2 > 0;
        }
        return true;
    }

    public static boolean bulletInBounds(Body body) {
        UserData userData = (UserData) body.getUserData();
        switch (userData.getUserDataType()) {
        case BULLET:
            return body.getPosition().x + userData.getWidth() < 20;
        }
        return true;
    }
Rimwis
  • 33
  • 2
  • 7
  • Are you doing this while `world.step` is running? That means in a callback for example? – noone Sep 16 '14 at 07:36
  • Yes, the world step is running in the same method where i'm doing for loop – Rimwis Sep 16 '14 at 07:42
  • it's in the same method, or it's running in parallel? – noone Sep 16 '14 at 08:05
  • You could try delaying the removal: `Gdx.app.postRunnable(new Runnable() {@Override public void run () {world.destroyBody(toRemove);}});` – noone Sep 16 '14 at 08:06
  • same method, "postRunnable" didn't worked, to be more clear about the problem : the situation is this i got 3 type of bodies : enemy, player and bullet , enemy has -10f velocity and bullet has 10f velocity, when enemy goes of bounds ( less then 0 coordinate) it is destroyed but if i shoot at the same time when enemy is destroyed ( and post 2-3s) my bullet velocity goes from 10f to 0 ( dont even know why ) and after second tap my program crashes with a fatal error : EXCEPTION_ACCESS_VIOLATION (0xc0000005). – Rimwis Sep 16 '14 at 08:36
  • Okay , after lots of testing i reached another conclusion , so basically i tried to to debug without shooting and i get same error but on different situation : when my 1 enemy is destroyed and other created on the same time my game crashes, and don't know why. – Rimwis Sep 16 '14 at 15:45

1 Answers1

0

Fixed it , createEnemy() method placed after world.destroyBody(body) method.

Rimwis
  • 33
  • 2
  • 7