Xna 在返回行走状态之前,如何先在Update_Hit函数中完成精灵动画?
这是我用来切换状态的代码:Xna 在返回行走状态之前,如何先在Update_Hit函数中完成精灵动画?,xna,xna-4.0,Xna,Xna 4.0,这是我用来切换状态的代码: if (mCurrentState == State.Walking) { action = "stand"; Update_Stand(gameTime); if (aCurrentKeyboardState.IsKeyDown(Keys.Left) == true) { action = "run"; feetPosition.X +=
if (mCurrentState == State.Walking)
{
action = "stand";
Update_Stand(gameTime);
if (aCurrentKeyboardState.IsKeyDown(Keys.Left) == true)
{
action = "run";
feetPosition.X += MOVE_LEFT;
effect = SpriteEffects.None;
Update_Run(gameTime);
}
else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) == true)
{
action = "run";
feetPosition.X += MOVE_RIGHT;
effect = SpriteEffects.FlipHorizontally;
Update_Run(gameTime);
}
if (aCurrentKeyboardState.IsKeyDown(Keys.Z) == true)
{
mCurrentState = State.Hitting;
}
}
if (mCurrentState == State.Hitting)
{
action = "hit";
Update_Hit(gameTime);
mCurrentState = State.Walking;
}
我的Update_Hit(GameTime GameTime)方法是这样的。我有两个精灵的动画
public void Update_Hit(GameTime gameTime)
{
// Check if it is a time to progress to the next frame
if (nextFrame_Hit >= frameInterval_Hit)
{
// Progress to the next frame in the row
currentFrame_Hit.X++;
// If reached end of the row advance to the next row, reset to the first frame
if (currentFrame_Hit.X >= sheetSize_Hit.X)
{
currentFrame_Hit.X = 0;
currentFrame_Hit.Y++;
}
// If reached last row in the frame sheet, jump to the first row again
if (currentFrame_Hit.Y >= sheetSize_Hit.Y)
currentFrame_Hit.Y = 0;
// Reset time interval for next frame
nextFrame_Hit = TimeSpan.Zero;
}
else
{
// Wait for the next frame
nextFrame_Hit += gameTime.ElapsedGameTime;
}
}
在将状态更改回行走之前,如何使命中动画完成?由于您使用的是固定的游戏时间,您需要确保在动画完成之前没有更改状态,以确保在每次更新时都能获得动画的一帧,直到动画完成:
if (nextFrame_Hit >= frameInterval_Hit)
{
// Progress to the next frame in the row
currentFrame_Hit.X++;
// If reached end of the row advance to the next row, reset to the first frame
if (currentFrame_Hit.X >= sheetSize_Hit.X)
{
currentFrame_Hit.X = 0;
currentFrame_Hit.Y++;
}
// If reached last row in the frame sheet, jump to the first row again
if (currentFrame_Hit.Y >= sheetSize_Hit.Y)
{
currentFrame_Hit.Y = 0;
mCurrentState = State.Walking;
}
// Reset time interval for next frame
nextFrame_Hit = TimeSpan.Zero;
}
else
{
// Wait for the next frame
nextFrame_Hit += gameTime.ElapsedGameTime;
}
您需要更改为:
if (mCurrentState == State.Hitting)
{
action = "hit";
Update_Hit(gameTime);
}
frameInterval\u Hit是否曾经达到零?//帧之间的时间量是TimeSpan frameInterval\u Hit;//从最后一帧开始经过的时间是TimeSpan nextFrame\u命中时间;因此,您希望运行动画,直到它穿过所有行,并且只要按住z关键点,就希望它循环帧集;这听起来对吗?我想运行动画,直到它通过所有的行,即使z键不再被按住。动画完成后,它会返回到行走状态。我想我看到了你在尝试做什么。它不起作用。当我保持按住Z键时,动画卡在那里,在我释放Z键后,它会返回到行走状态。所以它不会设置动画,它会卡在第一帧上?是的。它贴在第一帧上。我只是为了方便而共享代码。这很难解释。我做了一个很小的改变。有什么区别吗?