opposite of you
opposite of you

Reputation: 1365

Why is my collision detection unpredictable?

I am currently building a game in AndEngine, but my collision detection seems a bit off. It works.. most of the time, but seems like when there is a collision with one object, it wont do the other. It's hard to explain and it's very unpredictable.

for (int i = 0; i < rManager.carArray.length; i++)
{
    if (rManager.getInstance().snowArray[0].getSnowSprite().collidesWith(rManager.getInstance().carArray[i].getCarSprite()))
    {
        Log.e("SNOW", "snow 0 collided with " + rManager.getInstance().carArray[i].ToString());
        rManager.getInstance().carArray[i].setCarSpeed(0.1f);
        break;
    }

    if (rManager.getInstance().iceArray[0].getIceSprite().collidesWith(rManager.getInstance().carArray[i].getCarSprite()))
    {
        Log.e("ICE", "ice 0 collided with " + rManager.getInstance().carArray[i].ToString());
        rManager.getInstance().carArray[i].setCarSpeed(1f);
        break;
    }

    else
    {
        rManager.getInstance().carArray[i].setCarSpeed(0.5f);
    }
}

Is there anything wrong with my code? Currently, both enemy arrays only have 1 element. That is why I am only checking 0.

Upvotes: 0

Views: 59

Answers (1)

Plastic Sturgeon
Plastic Sturgeon

Reputation: 12527

It is because of the "break" keyword. When that is called it "breaks out" of the surrounding for loop. So if the first if statement detects a collision, it will not ever call the second if statement.

Upvotes: 2

Related Questions