Question

Whenever my game hits a score of 3, it crashes. After much reviewing, I noticed that it has to do with this code because I would remove it and then it wouldn't crash anymore. The weirdest part about it, is that it would only crash after playing for a while. I can play a new game, it would be more than 3 and not crash. I don't understand why it only happens once in a while The error code I then get is:

Assertion failed: (m_nodes[proxyId].IsLeaf()), function DestroyProxy, file /Users/badlogic/jenkins/workspace/libgdx-mac/gdx/jni/Box2D/Collision/b2DynamicTree.cpp, line 127.

Code:

int j = Asteroids.bodies.size;
    if(j == 9 && p == 0) {
        score ++;
        p ++;
        store.coins.putInteger("coins", (store.coins.getInteger("coins", 0) + 1));
    }
    if(j == 10 && p == 1) {
        score ++;
        p ++;
        store.coins.putInteger("coins", (store.coins.getInteger("coins", 0) + 1));
    }
    if(j == 11 && p == 2) {
        score ++;
        p ++;
        store.coins.putInteger("coins", (store.coins.getInteger("coins", 0) + 1));
    }
    try {
        if(j > 11) {
            for(int i = 0; i < Asteroids.bodies.size - 11; score ++) {
                world.destroyBody(Asteroids.bodies.get(i));
                Asteroids.bodies.removeIndex(i);
                store.coins.putInteger("coins", (store.coins.getInteger("coins", 0) + 1));
            }
        }
    }
    catch(NullPointerException e) {
        System.out.println("oops");
    }
}
Was it helpful?

Solution

Why is it related to the score: because you only start running the code when asteroids.bodies.size>11 - this presumably only happens after you've shot something up.

In general it is not safe to remove items from a collection that are currently iterating over - without the declaration of asteroids.bodies it is difficult to see whether this is causing your problem. But something along the lines of:

while (there are more bodies than I want)
  remove the first body in the list

is generally used for this situation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top