我现在所拥有的是一个动画,当我按下空格时开始:
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;
}
}
答案 0 :(得分:2)
在更新中,检查当前动画是否正在moveRightSwingingSword,如果是,则检查它是否已结束(猜测持续时间是否可用于此)。
我认为它可能是这样的:
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;
}