0

我现在拥有的是一个动画,当我按下空格时开始:

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

但是,当我停止单击空间时,我的动画会继续。在我移动我的播放器之前,我该如何让它在你放开空间时立即停止动画?

几行

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;
     }

完整代码

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;
    }
}
4

1 回答 1

2

在更新中,检查当前动画是否为movingRightSwingingSword,如果是,则检查它是否已结束(猜测可以使用持续时间)。

我认为它可能是这样的:

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
   if(input.isKeyDown(Input.KEY_SPACE)) player = movingRightSwingingSword;
   //This is the new code, I don´t know what are the real functions to be used, but the concept is this
   if (player == movingRightSwingingSword && player.ended()) player = defaultAnimation;
}

编辑:如果你想在空格键被释放时改变它,只需在条件的 else 部分改变动画:

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
       if(input.isKeyDown(Input.KEY_SPACE)) player = movingRightSwingingSword;
       else if (player == movingRightSwingingSword) player = defaultAnimation;
    }
于 2012-06-15T01:43:58.663 回答