gpt4 book ai didi

java - 松开按键后停止动画

转载 作者:行者123 更新时间:2023-12-02 07:44:51 25 4
gpt4 key购买 nike

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

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

最佳答案

在更新中,检查当前动画是否正在 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;
}

关于java - 松开按键后停止动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11043438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com