0
votes

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;
    }
1
Are you doing this while world.step is running? That means in a callback for example?noone
Yes, the world step is running in the same method where i'm doing for loopRimwis
it's in the same method, or it's running in parallel?noone
You could try delaying the removal: Gdx.app.postRunnable(new Runnable() {@Override public void run () {world.destroyBody(toRemove);}});noone
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

1 Answers

0
votes

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