Quantcast
Channel: Question and Answer » libgdx
Viewing all articles
Browse latest Browse all 434

Why does this simple if-statement that checks my main characters state never get called (Java/LibGDX)?

$
0
0

Been working on this for a while – basically I want my player to perform a front flip when he jumps (screen tap -> player jumps). I wrote the animation class Animator (extends Player implements ApplicationListener and my plan is to initialise it in Player (main character class) whenever his state becomes JUMPING. I did a few tests using System.print and found that when the screen is tapped (i.e. whenever touchDown() is called) the Player’s state IS modified (using stateModifier(“jumping”).

However, the code inside if(state == playerState.JUMPING) in update() for some reason is never called. I know this is the case because the System.print I wrote never gets outputted. I’ll throw my code in here since you will probably need to see the big picture:

public class Player extends GameObject implements InputProcessor {
private Animator jumpAnimation; // the class where the animation takes place    
private playerState state; // enum of player states
private float gravity;
private boolean allowJump;
private float accelerationJump;
private Texture img;
private Sprite sprite;

public Player(float xPos, float yPos) {
    super(xPos, yPos);
    img = new Texture(Gdx.files.internal("JUMP1.png"));
    sprite = new Sprite(img);
    setxPos(xPos);
    setyPos(yPos);
    setxSpeed(4.6f);
    setySpeed(0);
    sprite.setX(getxPos());
    sprite.setY(getyPos());
    setType("player");
    gravity = 0.34f;
    accelerationX = 0.02f;
    accelerationJump = 14f;
    allowJump = true;
    Gdx.input.setInputProcessor(this);
    state = playerState.NORMAL; 
}

// change the player state
public void stateModifier(String modify) {
    if (modify.equalsIgnoreCase("jetpack")) {
        state = playerState.JETPACK_MODE;
    } else if(modify.equalsIgnoreCase("normal")) {
        state = playerState.NORMAL;
    } else if(modify.equalsIgnoreCase("jumping")) {
        state = playerState.JUMPING;
        System.out.println(state);
}
}

public void update() {

    // Player's speed in the y plane is affected by gravity
    setySpeed(getySpeed() - gravity);
    // Player constantly moving
    moveBy(getxSpeed(), getySpeed());
    // Don't allow Player to fall below the ground due to gravity
    if (belowGround()) {
        moveTo(getxPos(), groundY);
    }
    // If Player is on the ground then he can jump, so set allowJump to true
    if (onGround()) {
        allowJump = true;
        setySpeed(0);
    }
    // if the player is in a jumping state, run the jump animation
    if (state == playerState.JUMPING) {
        // this print statement should get called when a player jumps but never does
        System.out.println("should animate right now");
        jumpAnimation = new Animator(getxPos(), getyPos());
    }
}

public void jump() {
    setySpeed(getySpeed() + accelerationJump);
}

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    // if the player is in a normal state (i.e. not jumping) and he is allowed to jump
    if (state == playerState.NORMAL && allowJump) {
        // set player state to playerState.JUMPING
        stateModifier("jumping"); 
        jump();
                    // set player state to playerState.NORMAL after the jump
        stateModifier("normal");
        allowJump = false;
    }
    return true;
}

}

Any help is highly appreciated, thank you very much.

tl;dr when screen is tapped, touchDown() is called, which changes the Player’s state to JUMPING. however when i write an if statement in update() to check for this, it never gets executed. why is this the case?


Viewing all articles
Browse latest Browse all 434

Trending Articles