我正在使用libGDX构建一个小型游戏,目前正在尝试在屏幕上制作动画。 当前,它仅显示动画的第一帧。 我正在使用线程来更新和渲染(这是我怀疑问题所在的地方) 这是要动画的对象的类 我知道我可能在这里错过了一些非常大而明显的东西。 ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我出于学习目的而制作2D塔防游戏,并且在移动时无法使敌人(精灵)面对正确的方向。
以下是地图的构建方式,以进行澄清:
http://i.imgur.com/ivO8kWe.png
这是我用来测试的Sprite工作表:
http://i.imgur.com/2h4fSL3.png
左上方的第一个子画面是第0帧,右侧的下一个子画面是第1帧,依此类推。 如您所见,精灵已经朝错误的方向前进。 该地图具有起点(顶部的第一个棕色瓷砖)和终点(终点的最后一个棕色瓷砖),并且只有棕色瓷砖是可步行的,因此有了起点和终点,它将计算出精灵走到终点的最短有效路径。
就是说,每一个生成的精灵都会有一个预定的行走路径,从中我尝试通过检查最后的X或Y位置以及X或Y当前位置来找到精灵所面对的方向,并选择哪条线我将用于行走动画的Sprite表的。 例如,假设小精灵正在向南移动,它应该使用小精灵表格底部(第15到19帧)的小精灵,但它不起作用。
这是我用于敌人的动画课:
public class AnimatedSprite : Sprite
{
public int Lines { get; set; }
public int Columns { get; set; }
protected int currentFrame;
protected int totalFrames;
protected int timeSinceLastFrame = 0;
protected int milisecondsPerFrame = 50;
public AnimatedSprite(Texture2D texture, int lines, int columns, Vector2 position)
: base ( texture, position)
{
this.texture = texture;
this.position = position;
Lines = lines;
Columns = columns;
totalFrames = Lines * Columns;
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
//Here i check if the sprite sheet have more than 1 line because if it have,
//it must use a different update method.
if (Lines > 1)
{
// Down
if (lastPostion.Y < position.Y)
{
AnimateDown(gameTime);
}
// Up
if (position.Y < lastPosition.Y)
{
AnimateUp(gameTime);
}
// Right
if (position.X > lastPosition.X)
{
AnimateRight(gameTime);
}
// Left
if (position.X < lastPosition.X)
{
AnimateLeft(gameTime);
}
}
if (Lines == 1) {
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > milisecondsPerFrame)
{
timeSinceLastFrame -= milisecondsPerFrame;
currentFrame++;
if (currentFrame == totalFrames)
{
currentFrame = 0;
}
}
}
center = new Vector2(position.X + texture.Width / Columns, position.Y + texture.Height / Lines);
}
public void AnimateUp(GameTime gameTime)
{
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > milisecondsPerFrame)
{
timeSinceLastFrame -= milisecondsPerFrame;
currentFrame++;
if (currentFrame > 14)
currentFrame = 10;
}
}
public void AnimateDown(GameTime gameTime)
{
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > milisecondsPerFrame)
{
timeSinceLastFrame -= milisecondsPerFrame;
currentFrame++;
if (currentFrame > 19)
currentFrame = 15;
}
}
public void AnimateLeft(GameTime gameTime)
{
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > milisecondsPerFrame)
{
timeSinceLastFrame -= milisecondsPerFrame;
currentFrame++;
if (currentFrame > 4)
currentFrame = 0;
}
}
public void AnimateRight(GameTime gameTime)
{
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > milisecondsPerFrame)
{
timeSinceLastFrame -= milisecondsPerFrame;
currentFrame++;
if (currentFrame > 9)
currentFrame = 5;
}
}
public override void Draw(SpriteBatch spriteBatch)
{
int width = texture.Width / Columns;
int height = texture.Height / Lines;
int line = (int)((float)currentFrame / (float)Columns);
int column = currentFrame % Columns;
Rectangle originRectangle = new Rectangle(width * column, height * line, width, height);
Rectangle destinationRectangle = new Rectangle((int)position.X, (int)position.Y, width, height);
spriteBatch.Draw(texture, destinationRectangle, originRectangle, Color.White);
}
}
}
编辑:我在一条直线水平上进行了测试(起点在左侧),它开始朝左(第0帧),但是当它到达第三个棕色瓷砖时,它固定并面向正确的方向:
i.imgur.com/3FsGhuY.png
Edit2:我在所有四个方向(上下移动,向右移动和向左移动,反之亦然)上使用直线水平进行了测试,并且在所有这些方向上,它都从第0帧开始,并在到达第三个图块时开始修复并面对正确的方向。
您无需检查当前帧是否低于所需动画循环的最小值; 您只需要对照最大值进行检查即可。 另外,您的代码中有一些重复可能应该被考虑在内,以使其更易于阅读和使用。
我将用单个方法替换所有AnimateXXXX方法:
public void AnimateLoop(GameTime gameTime, int loopFirstFrame, int loopLastFrame)
{
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > milisecondsPerFrame)
{
timeSinceLastFrame -= milisecondsPerFrame;
currentFrame++;
}
if (currentFrame > loopLastFrame || currentFrame < loopFirstFrame)
currentFrame = loopFirstFrame;
}
然后像这样称呼他们:
// Down
if (lastPostion.Y < position.Y)
AnimateLoop(gameTime, 15, 19);
// Up
if (position.Y < lastPosition.Y)
AnimateLoop(gameTime, 10, 14);
// Right
if (lastPosition.X < position.X)
AnimateLoop(gameTime, 5, 9);
// Left
if (position.X < lastPosition.X)
AnimateLoop(gameTime, 0, 4);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.