Stopping the animation after you let go of a key

Posted by Michael Zeuner on Stack Overflow See other posts from Stack Overflow or by Michael Zeuner
Published on 2012-06-15T01:09:53Z Indexed on 2012/06/15 21:16 UTC
Read the original article Hit count: 217

Filed under:
|
|
|

What I have right now is an animation that starts when I press space:

if(input.isKeyDown(Input.KEY_SPACE)) player = movingRightSwingingSword;

However when I stop clicking space my animation continues. until I move my player, how do I make it so it stops the animation right when you let go of space?

A few lines

Animation player, movingUp, movingDown, movingLeft, movingRight, movingRightSwingingSword;
int[] duration = {200,200};

public void init(GameContainer gc, StateBasedGame sbg) throws SlickException
    {
    Image[] attackRight = {new Image("res/buckysRightSword1.png"), new Image("res/buckysRightSword2.png")};

    movingRightSwingingSword = new Animation(attackRight, duration, true);
    }

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException
    {
     if(input.isKeyDown(Input.KEY_SPACE)) player = movingRightSwingingSword;
     }

Full Code

package javagame;

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Play extends BasicGameState
{
    Animation player, movingUp, movingDown, movingLeft, movingRight, movingRightSwingingSword;
    Image worldMap;
    boolean quit = false;
    int[] duration = {200,200};
    float playerPositionX = 0;
    float playerPositionY = 0;
    float shiftX = playerPositionX + 320;
    float shiftY = playerPositionY + 160;

    public Play(int state)
    {

    }

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException
    {
        worldMap = new Image("res/world.png");
        Image[] walkUp = {new Image("res/buckysBack.png"), new Image("res/buckysBack.png")};
        Image[] walkDown = {new Image("res/buckysFront.png"), new Image("res/buckysFront.png")};
        Image[] walkLeft = {new Image("res/buckysLeft.png"), new Image("res/buckysLeft.png")};
        Image[] walkRight = {new Image("res/buckysRight.png"), new Image("res/buckysRight.png")};
        Image[] attackRight = {new Image("res/buckysRightSword1.png"), new Image("res/buckysRightSword2.png")};

        movingUp = new Animation(walkUp, duration, false);
        movingDown = new Animation(walkDown, duration, false);
        movingLeft = new Animation(walkLeft, duration, false);
        movingRight = new Animation(walkRight, duration, false);
        //doesnt work! vvv
        movingRightSwingingSword = new Animation(attackRight, duration, true);
        player = movingDown;

    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException
    {
        worldMap.draw(playerPositionX, playerPositionY);
        player.draw(shiftX, shiftY);
        g.drawString("Player X: " + playerPositionX + "\nPlayer Y: " + playerPositionY, 400, 20);

        if (quit == true)
        {
            g.drawString("Resume (R)", 250, 100);
            g.drawString("MainMenu (M)", 250, 150);
            g.drawString("Quit Game (Q)", 250, 200);
            if (quit==false)
            {
                g.clear();
            }
        }
    }

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException
    {
        Input input = gc.getInput();

        if(input.isKeyDown(Input.KEY_UP)) 
        {
            player = movingUp;
            playerPositionY += delta * .1f;
            if(playerPositionY>162) playerPositionY -= delta * .1f; 
        }

        if(input.isKeyDown(Input.KEY_DOWN)) 
        {
            player = movingDown;
            playerPositionY -= delta * .1f;
            if(playerPositionY<-600) playerPositionY += delta * .1f; 
        }

        if(input.isKeyDown(Input.KEY_RIGHT)) 
        {
            player = movingRight;
            playerPositionX -= delta * .1f;
            if(playerPositionX<-840) playerPositionX += delta * .1f; 
        }

        if(input.isKeyDown(Input.KEY_LEFT)) 
        {
            player = movingLeft;
            playerPositionX += delta * .1f;
            if(playerPositionX>318) playerPositionX -= delta * .1f; 
        }

        if(input.isKeyDown(Input.KEY_SPACE)) player = movingRightSwingingSword;

        if(input.isKeyDown(Input.KEY_ESCAPE)) quit = true;
        if(input.isKeyDown(Input.KEY_R)) if (quit == true) quit = false;
        if(input.isKeyDown(Input.KEY_M)) if (quit == true) {sbg.enterState(0); quit = false;}
        if(input.isKeyDown(Input.KEY_Q)) if (quit == true) System.exit(0);
    }

    public int getID()
    {
        return 1;
    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about animation