0
\$\begingroup\$

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?

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

It is never called because in the touchDown function you are calling stateModifier("normal") right after stateModifier("jumping"), so the state will be set to NORMAL when the function ends. In the update function you are testing if the state is equal to JUMPING, but is not, it will be NORMAL.

\$\endgroup\$
1
\$\begingroup\$

I guess you're switching to "normal" state too early. In fact, you're doing it right after you switch to "jumping" state, with just one instruction inbetween, so actually its like continuos "normal" state, "jumping" is to short to ever get noticed by render method. Jumps should last some time, and then you should switch back to "normal" state after you land on the ground again.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .