foopydoop
foopydoop

Reputation: 35

If statement seems to run some code but not all in java

I'm making a 2d platformer in libgdx with box2d. The below is the update method for one of my enemies. It's an extension of the 'Enemy' class, which is what 'super.update' refers to. I want the enemy to run when the player is behind it or far away and to stop and shoot when the player is close to it and in front of it.

I try to achieve this by setting the speed (velocity.x) initially [depending on the enemy's direction], then setting whether or not it's shooting afterwards.

The problem I have at the moment is that the enemy doesn't run when the player is behind it. As you can see, I printed out a lot of strings to console to see when the velocity.x gets changed back to 0. According to the console, it happens in the last if/else pair of statements which are supposed to check how far away the player is and which direction the enemy is running. However, the console strings within those statements, the ones that say 'Shoot Left' or 'Shoot Right', don't get printed out. Despite this, the line that changes velocity.x must get run because the it's value changes according to the string output in the next line. The if statements at the top which check direction must get run as well because the console outputs within those statements get printed, and the output that says the velocity says the correct velocity (either 2 or -2).

What is going on? It seems like the IDE is running only one of the lines in the if statement. That's impossible so what am I missing here?

Thanks for any help.

public void update (float dt, Player player){
    super.update(dt, player);
    if (b2body.isActive()){
        System.out.println(b2body.getPosition().x - player.b2body.getPosition().x);
        System.out.println("After Enemy code: " + velocity.x);
        if (getRunningRight()) {
            System.out.println("Right");
            velocity.x = 2;
        }
        else if (!getRunningRight()) {
            System.out.println("Left");
            velocity.x = -2; 
        }
        System.out.println("After checking direction: " + velocity.x);

        if ((b2body.getPosition().x - player.b2body.getPosition().x <= 2 &&
                b2body.getPosition().x - player.b2body.getPosition().x >= 0) && !getRunningRight()){
            velocity.x = 0;
            System.out.println("Shoot left");
        }
        else if ((b2body.getPosition().x - player.b2body.getPosition().x >= -2 &&
                b2body.getPosition().x - player.b2body.getPosition().x < 0) && getRunningRight()){
            System.out.println("Shoot right");
            velocity.x = 0;
        }
        System.out.println("After shooting: " + velocity.x);
    }
}

Upvotes: 0

Views: 74

Answers (1)

Scott Hunter
Scott Hunter

Reputation: 49883

In your first set of ifs, you evaluate getRunningRight() after you have already determined that it will be false (by the initial if failing), so there is no need to evaluate it again.

If you think you are doing the same thing in the second block, you are not; the expression in the inner if is not the opposite of the first one. That is, (A && B) && C is not the opposite of (!A && !B) && !C. Thus, it is possible for both expressions to be false.

Upvotes: 3

Related Questions