如何检测精灵在哪个方向移动?

时间:2014-12-03 18:12:20

标签: c# xna

我正在制作用于学习目的的2D塔防游戏,现在我陷入了如何让敌人在移动时朝正确的方向看。

这是我尝试没有成功的原因:

    // Class constructor
    public AnimatedSprite(Texture2D texture, int lines, int columns, Vector2 position)
        : base ( texture, position)
    {
        this.texture = texture;
        this.position = position;
        Lines = lines; // How many lines the sprite-sheet have
        Columns = columns; How many columns the sprite-sheet have
        totalFrames = Lines * Columns;
    }

这是更新方法,它是一个5x4精灵表:

    public override void Update(GameTime gameTime)
    {

        // Depending on the direction the sprite is moving
        // it will use different parts of the sprite-sheet

        if (west == true)
        {
            initialFrame = 0;
            finalFrame = 4;
        }

        if (east == true)
        {
            initialFrame = 5;
            finalFrame = 9;
        }

        if (north == true)
        {
            initialFrame = 10;
            finalFrame = 14;
        }

        if (south == true)
        {
            initialFrame = 15;
            finalFrame = 19;
        }

        // When to update the current frame
        timeSinseLastFrame += gameTime.ElapsedGameTime.Milliseconds;
        if (timeSinceLastFrame > milisecondsPerFrame)
        {
            timeSinceLastFrame -= milisecondsPerFrame;
            currentFrame++;

         // Reset the frame to complete the loop
            if (currentFrame == finalFrame)
                currentFrame = initialFrame;
        }
    }

在绘制方法中,我尝试检测精灵所面对的位置,我不确定最好的方法,但它不起作用,敌人被正确绘制和动画但有时在地图上面向错误的方向路径,在某一点上它们消失了:

    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);


        // Heres what i think it's not correct, how to detect the direction
        // I tried to calculate it by the last X and Y position
        // So if the current Y value is smaller the last Y value
        // The sprite moved south
        Rectangle lastPosition = originRectangle;
        Rectangle destinationPosition = destinationRectangle;

        if (lastPosition.Y < destinationPosition.Y)
        {
            north = true;
            south = false;
            east = false;
            west = false;
        }

        if (destinationRectangle.Y > lastPosition.Y)
        {
            north = false;
            south = true;
            east = false;
            west = false;
        }

        if (destinationRectangle.X > lastPosition.X)
        {
            north = false;
            south = false;
            east = true;
            west = false;
        }

        if (destinationRectangle.X < lastPosition.X)
        {
            north = false;
            south = false;
            east = false;
            west = true;
        }

        spriteBatch.Draw(texture, destinationRectangle, originRectangle, Color.White);

    }
}
}

2 个答案:

答案 0 :(得分:0)

首先看一下你的代码段:

if (lastPosition.Y < destinationPosition.Y)
{
    north = true;
    south = false;
    east = false;
    west = false;
}

if (destinationRectangle.Y > lastPosition.Y)
{
    north = false;
    south = true;
    east = false;
    west = false;
}

(lastPosition.Y&lt; destinationPosition.Y)和(destinationRectangle.Y&gt; lastPosition.Y) 条件相同,所以在两种情况下你都会有南=真。

其次,为什么要使用四个布尔变量?而是使用一个具有四种可能状态的变量。 Enum很适合这个:

enum Direction
{
    None = 0, // default
    North = 1,
    South = 2,
    East = 3,
    West = 4
}

而不是四个布尔值使用一个Direction变量。减少变量就少了麻烦。

最后。 我不明白你怎么移动你的精灵?我的意思是你在更换坐标之前如何检测方向? 在我看来,它应该是这样的:

  1. 在Update()中检测您需要移动的方向并更改Direction varialbe并在此基础上更改并更新精灵状态。
  2. 在Draw()中,在当前状态下绘制精灵。

答案 1 :(得分:0)

我解决了它,我从基类中选择位置并更新基类中的最后位置,并且在敌人类中,update方法将根据位置处理它将使用的帧。

敌人类更新方法:

            if (lastPosition.Y < position.Y)
            {
                initialFrame = 15;
                lastFrame = 19;
            }

            if (position.Y < lastPosition.Y)
            {
                initialFrame = 10;
                lastFrame = 14;
            }

            if (position.X > lastPosition.X)
            {
                initialFrame = 5;
                lastFrame = 9;
            }

            if (position.X < lastPosition.X)
            {
                initialFrame = 0;
                lastFrame = 4;
            }

            currentFrame = initialFrame;
            totalFrame = lastFrame;

            timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
            if (timeSinceLastFrame > milisecondsPerFrame)
            {
                timeSinceLastFrame -= milisecondsPerFrame;
                currentFrame++;
                if (currentFrame == totalFrame)
                    currentFrame = 0;
            }       
        }