使用一个具有四个方向的精灵表的精灵的2D运动动画

时间:2015-01-14 19:17:30

标签: c# xna 2d

我正在制作用于学习目的的2D塔防游戏,并且不能让敌人(精灵)在移动时面向正确的方向。

下面是如何构建地图以澄清:

http://i.imgur.com/ivO8kWe.png

这是我用来测试的精灵表:

http://i.imgur.com/2h4fSL3.png

左上角的第一个精灵是第0帧,右边的第一个精灵是第1帧,依此类推。正如您所看到的,精灵已经在寻找错误的方向。地图有一个起始点(顶部的第一个棕色瓷砖)和终点(最后一个棕色瓷砖),只有棕色瓷砖可以走路,所以有了起点和终点,它将计算出精灵步行以达到终点的最短有效路径。

说,每个产生的精灵都会有一个预先确定的行走路径,从那里我试图通过检查最后的X或Y位置和X或Y当前位置来找到精灵所面对的方向,然后我选择我将用于步行动画的精灵表格的哪一行。例如,让我们说精灵向南移动,它应该使用精灵表底部的精灵(第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帧开始,当它到达时第三个瓷砖它修复并面向正确的方向。

1 个答案:

答案 0 :(得分:1)

您没有检查当前帧是否低于所需动画循环的最小值;你只是按照最大值来检查它。此外,您的代码中有一些重复,可能会被考虑在内,以便于阅读和使用。

我会用一种方法替换所有的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);