C# XNA-碰撞时在原地播放动画,到达最后一帧时删除动画
在我的乒乓球比赛中,我有两个桨和一个球。我试图这样做,当球与桨碰撞时,会显示一个效果/动画。我有一个Spriteheet和一个工作动画。我使用它来检测动画何时播放(请注意,我还没有确定效果的位置,所以它在400300上播放只是为了看看它是否有效) 在我的画中:C# XNA-碰撞时在原地播放动画,到达最后一帧时删除动画,c#,animation,xna,effects,C#,Animation,Xna,Effects,在我的乒乓球比赛中,我有两个桨和一个球。我试图这样做,当球与桨碰撞时,会显示一个效果/动画。我有一个Spriteheet和一个工作动画。我使用它来检测动画何时播放(请注意,我还没有确定效果的位置,所以它在400300上播放只是为了看看它是否有效) 在我的画中: protected override void Draw(GameTime gameTime) { spriteBatch.Begin(); if (BallHitEffect()) { animatedSp
protected override void Draw(GameTime gameTime)
{
spriteBatch.Begin();
if (BallHitEffect())
{
animatedSprite.Draw(spriteBatch, new Vector2(400, 250));
}
}
现在它在碰撞时出现,但只出现了短短的毫秒,因为当球离开桨时,它就消失了。我知道它被编码为仅在与划桨碰撞时出现,但我如何才能使它仅在动画结束时消失?您可以轻松地使用经过的时间和一些临时计时器来完成此操作。我假设您的精灵表是一系列帧,并且您已经设置了渲染 开始时需要一些变量:
double frameTimePlayed; //Amount of time (out of animationTime) that the animation has been playing for
bool IsPlaying; //Self-Explanitory
int frameCount; //Frames in your sprite, I'm sure you already handle this in your animation logic, but I just threw it in here
int animationTime = 1000; //For 1 second of animation.
在更新方法中,您需要
IsPlaying
设置为true正在播放
,则将播放的帧时间增加经过的时间
frametimes
等于或大于animationTime
,请通过将isplay
设置为false来停止动画isplay
protected override void Update(GameTime gameTime)
{
//If it is not already playing and there is collision, start playing
if (!IsPlaying && BallHitEffect)
IsPlaying = true;
//Increment the frameTimePlayed by the time (in milliseconds) since the last frame
if (IsPlaying)
frameTimePlayed += gameTime.ElapsedGameTime.TotalMilliseconds;
//If playing and we have not exceeded the time limit
if (IsPlaying && frameTimePlayed < animationTime)
{
// TODO: Add update logic here, such as animation.Update()
// And increment your frames (Using division to figure out how many frames per second)
}
//If exceeded time, reset variables and stop playing
else if (IsPlaying && frameTimePlayed >= animationTime)
{
frameTimePlayed = 0;
IsPlaying = false;
// TODO: Possibly custom animation.Stop(), depending on your animation class
}
}
受保护覆盖无效更新(游戏时间游戏时间)
{
//如果尚未播放且发生碰撞,则开始播放
如果(!显示和显示效果(&P)
isplay=true;
//按自最后一帧起的时间(以毫秒为单位)递增FrameTimePlay
如果(显示)
frameTimePlayed+=gameTime.ElapsedGameTime.TotalMillicons;
//如果玩的话我们还没有超过时间限制
如果(显示和帧时间播放<动画时间)
{
//TODO:在此处添加更新逻辑,例如animation.update()
//并增加帧数(使用除法计算每秒帧数)
}
//如果超过时间,重置变量并停止播放
else if(IsPlaying&&frameTimePlayed>=动画时间)
{
frameTimePlayed=0;
isplay=false;
//TODO:可能是自定义动画.Stop(),具体取决于动画类
}
}
对于绘图,非常简单,只要检查isIsPlaying
,如果是这种情况,就可以绘图
我确保对代码进行了很好的注释,希望这能帮助您更好地理解和工作
如果需要,您还可以使用
double
和TotalSeconds
来代替毫秒,并使用float-appeased=(float)gameTime.ElapsedGameTime.TotalSeconds计算经过的时间代码>您可以轻松地使用经过的时间和一些临时计时器来完成此操作。我假设您的精灵表是一系列帧,并且您已经设置了渲染
开始时需要一些变量:
double frameTimePlayed; //Amount of time (out of animationTime) that the animation has been playing for
bool IsPlaying; //Self-Explanitory
int frameCount; //Frames in your sprite, I'm sure you already handle this in your animation logic, but I just threw it in here
int animationTime = 1000; //For 1 second of animation.
在更新方法中,您需要
检查球是否相交,并将IsPlaying
设置为true
如果动画正在播放
,则将播放的帧时间增加经过的时间
如果播放的frametimes
等于或大于animationTime
,请通过将isplay
设置为false来停止动画
在绘制方法中,您需要
如果isplay
下面是一些示例代码:
protected override void Update(GameTime gameTime)
{
//If it is not already playing and there is collision, start playing
if (!IsPlaying && BallHitEffect)
IsPlaying = true;
//Increment the frameTimePlayed by the time (in milliseconds) since the last frame
if (IsPlaying)
frameTimePlayed += gameTime.ElapsedGameTime.TotalMilliseconds;
//If playing and we have not exceeded the time limit
if (IsPlaying && frameTimePlayed < animationTime)
{
// TODO: Add update logic here, such as animation.Update()
// And increment your frames (Using division to figure out how many frames per second)
}
//If exceeded time, reset variables and stop playing
else if (IsPlaying && frameTimePlayed >= animationTime)
{
frameTimePlayed = 0;
IsPlaying = false;
// TODO: Possibly custom animation.Stop(), depending on your animation class
}
}
受保护覆盖无效更新(游戏时间游戏时间)
{
//如果尚未播放且发生碰撞,则开始播放
如果(!显示和显示效果(&P)
isplay=true;
//按自最后一帧起的时间(以毫秒为单位)递增FrameTimePlay
如果(显示)
frameTimePlayed+=gameTime.ElapsedGameTime.TotalMillicons;
//如果玩的话我们还没有超过时间限制
如果(显示和帧时间播放<动画时间)
{
//TODO:在此处添加更新逻辑,例如animation.update()
//并增加帧数(使用除法计算每秒帧数)
}
//如果超过时间,重置变量并停止播放
else if(IsPlaying&&frameTimePlayed>=动画时间)
{
frameTimePlayed=0;
isplay=false;
//TODO:可能是自定义动画.Stop(),具体取决于动画类
}
}
对于绘图,非常简单,只要检查isIsPlaying
,如果是这种情况,就可以绘图
我确保对代码进行了很好的注释,希望这能帮助您更好地理解和工作
如果需要,您还可以使用double
和TotalSeconds
来代替毫秒,并使用float-appeased=(float)gameTime.ElapsedGameTime.TotalSeconds计算经过的时间代码>