0
\$\begingroup\$

I have a problem with stopping animations for a player in Slick2D. I have tried many solutions to the problem and have looked up a couple of resources but I am still stumped by the fact that it is not working. Here's the code I have for my class:

public SpriteSheet playerSS = null;
protected InputHandler inputHandler;
public float playerX, playerY = 250f;
private Animation sprite, runningAnimationLEFT, runningAnimationRIGHT, runningAnimationUP, runningAnimationDOWN;
private int duration = 200;

public Game (int stateID) {

}

@Override
public int getID() {
    return Main.game; //GAMESTATE = 1
}

@Override
public void init(GameContainer gameContainer, StateBasedGame stateBasedGame) throws SlickException {
    playerSS = new SpriteSheet("res/player_sprite_sheet.png", 50, 75);

    // ANIMATIONS
    runningAnimationDOWN = new Animation();
    runningAnimationDOWN.setAutoUpdate(true);
    runningAnimationDOWN.addFrame(playerSS.getSprite(1, 0), duration);
    runningAnimationDOWN.addFrame(playerSS.getSprite(0, 0), duration);
    runningAnimationDOWN.addFrame(playerSS.getSprite(2, 0), duration);

    runningAnimationLEFT = new Animation();
    runningAnimationLEFT.setAutoUpdate(true);
    runningAnimationLEFT.addFrame(playerSS.getSprite(1, 1), duration);
    runningAnimationLEFT.addFrame(playerSS.getSprite(0, 1), duration);
    runningAnimationLEFT.addFrame(playerSS.getSprite(2, 1), duration);

    runningAnimationRIGHT = new Animation();
    runningAnimationRIGHT.setAutoUpdate(true);
    runningAnimationRIGHT.addFrame(playerSS.getSprite(1, 2), duration);
    runningAnimationRIGHT.addFrame(playerSS.getSprite(0, 2), duration);
    runningAnimationRIGHT.addFrame(playerSS.getSprite(2, 2), duration);

    runningAnimationUP = new Animation();
    runningAnimationUP.setAutoUpdate(true);
    runningAnimationUP.addFrame(playerSS.getSprite(1, 3), duration);
    runningAnimationUP.addFrame(playerSS.getSprite(0, 3), duration);
    runningAnimationUP.addFrame(playerSS.getSprite(2, 3), duration);

    sprite = runningAnimationDOWN;
}

@Override
public void update(GameContainer gameContainer, StateBasedGame stateBasedGame, int delta) throws SlickException {
    inputHandler.updateKeyboardRelay(gameContainer);
    if (inputHandler.keyUP) {
        sprite = runningAnimationUP;
        sprite.update(delta);
        playerY -= delta * 0.1f;
    } else {
        // Stopping Animation goes here.
    }
    if (inputHandler.keyDOWN) {
        sprite = runningAnimationDOWN;
        sprite.update(delta);
        playerY += delta * 0.1f;
    } else {
        // Stopping Animation goes here.

    }
    if (inputHandler.keyLEFT) {
        sprite = runningAnimationLEFT;
        sprite.update(delta);
        playerX -= delta * 0.1f;
    } else {
        // Stopping Animation goes here.

    }
    if (inputHandler.keyRIGHT) {
        sprite = runningAnimationRIGHT;
        sprite.update(delta);
        playerX += delta * 0.1f;
    } else {
        // Stopping Animation goes here.

    }
}

@Override
public void render(GameContainer gameContainer, StateBasedGame stateBasedGame, Graphics graphics) throws SlickException {
    sprite.draw(playerX, playerY);
}

How would I go about stopping the animation from playing over and over again once the movement key is lifted? i.e. If someone presses the 'W' key the player does an animation going upwards, but if the user stops using the 'W' key how would I make it so that the animation stops playing as well?

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

I had a similar problem in my game. I used a simple boolean flag called isMoving that checked would default to false at the start of each render loop and would only become true if the player was pressing any of the keys.

Example

boolean isMoving;

public void update(BlahBlah parameterHere) {
    isMoving = false;

    if(keyispressed) {
        isMoving = true;
        // Other code
    }

    if(isMoving) {
        // Draw animation based on direction, direction can be stored in enum
    } else {
        // Draw static sprites based on direction, direction can be stored in enum
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Umm, that's a problem since I'm not very good with enums ;) Is there a decent reference I could get so that it could assist me in implementing the enum directions? \$\endgroup\$
    – BlueJet
    Dec 19, 2015 at 18:18
  • \$\begingroup\$ This should help: javarevisited.blogspot.co.uk/2011/08/… \$\endgroup\$ Dec 19, 2015 at 18:22

You must log in to answer this question.

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