Xna动画具有不同的高度,宽度
本文关键字:高度 宽度 动画 Xna | 更新日期: 2023-09-27 18:33:38
我目前正在用xna创建动画。当所有帧之间的帧高度、宽度和持续时间相同时,我对精灵表进行动画处理没有问题。
但是我发现它不是很灵活,我想在没有相同帧高度/宽度的精灵表的情况下进行动画制作,更重要的是没有恒定的持续时间。
是否有任何教程或代码示例来学习这一点?
编辑:我想到了这样的事情,但我无法让它工作:
public void Update(GameTime gameTime)
{
if (elapsedTime > frameTime)
{
currentFrame++;
elapsedTime = 0;
}
sourceRect = new Rectangle(newpositionX, newpositionY, newframeWidth, newframeHeight);
frametime = newframetime
}
enter code here
难点在于如何准确指定每帧的新位置和新帧时间。
如何为精灵表制作动画实际上取决于您。
本教程展示了一个带有 4x4 精灵表的跳舞笑脸。因此它有 16 帧:
| 1| 2| 3| 4|
| 5| 6| 7| 8|
| 9|10|11|12|
|13|14|15|16|
完成动画的循环(在 Update() 中):
public void Update()
{
currentFrame++;
if (currentFrame == totalFrames)
currentFrame = 0;
}
如果您只想使用奇数帧1,3,5,7,9,11,13,15
而不是偶数帧,那么您可以使用currentFrame+=2
而不是currentFrame++;
对于框架高度/宽度不同的精灵表,您可以使用缩放。如何在精灵表中导航取决于您,这意味着您不限于相同高度/宽度的精灵表。
至于持续时间,在我链接到的教程中,将尽可能快地更新笑脸的帧,因为没有实现控制来处理时间延迟(意味着每秒或每 .5 秒更新一次),因此在当前状态下,Draw() 方法每秒将被调用 60 次。
我对这个类做了
公共无效更新(游戏时间游戏时间) { currentAnimation = (currentAnimation + 1) % ExtractionXml.AnimationCount;
elapsedTime += (int)gameTime.ElapsedGameTime.TotalMilliseconds;
if (currentFrame == animationData.Animations[currentAnimation].Frames.Length)
{
if (Looping == true)
currentFrame = 0;
}
if (elapsedTime > animationData.Animations[currentAnimation].Frames[currentFrame].Duration*1000 && currentFrame <= animationData.Animations[currentAnimation].Frames.Length-1)
{
Height = animationData.Animations[currentAnimation].Frames[currentFrame].Height;
Width = animationData.Animations[currentAnimation].Frames[currentFrame].Width;
X = animationData.Animations[currentAnimation].Frames[currentFrame].X;
Y = animationData.Animations[currentAnimation].Frames[currentFrame].Y;
sourceRect = new Rectangle(X, Y, Width, Height);
//destinationRect = new Rectangle((int)Position.X - (int)(Width * scale) / 2, (int)Position.Y - (int)(Height * scale) / 2, (int)(Width * scale), (int)(Height * scale));
destinationRect = new Rectangle(scale* ((int)Position.X - (animationData.Animations[currentAnimation].Frames[currentFrame].Width / 2) + (animationData.Animations[currentAnimation].Frames[currentFrame].OffSetX))
, scale * ((int)Position.Y - (animationData.Animations[currentAnimation].Frames[currentFrame].Height / 2) + (animationData.Animations[currentAnimation].Frames[currentFrame].OffSetY))
, scale * animationData.Animations[currentAnimation].Frames[currentFrame].Width
, scale * animationData.Animations[currentAnimation].Frames[currentFrame].Height);
//destinationRect = new Rectangle(0 , 0 , animationData.Animations[currentAnimation].Frames[currentFrame].Width, animationData.Animations[currentAnimation].Frames[currentFrame].Height);
currentFrame++;
elapsedTime = 0;
}
}