首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何检测雪碧在向哪个方向移动?

如何检测雪碧在向哪个方向移动?
EN

Stack Overflow用户
提问于 2014-12-03 18:12:20
回答 2查看 105关注 0票数 0

我正在制作一个2D塔防御游戏,以学习的目的,现在我被困在如何使敌人看到正确的方向时,移动。

以下是我尝试过但没有成功的地方:

代码语言:javascript
复制
    // 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雪碧表:

代码语言:javascript
复制
    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;
        }
    }

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

代码语言:javascript
复制
    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);

    }
}
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-12-04 16:43:35

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

敌人等级更新方法:

代码语言:javascript
复制
            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;
            }       
        }
票数 0
EN

Stack Overflow用户

发布于 2014-12-04 11:46:36

首先看一看代码的这段:

代码语言:javascript
复制
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 < destinationPosition.Y)和(destinationRectangle.Y > lastPosition.Y)是相同的条件,因此在这两种情况下,您都将拥有south = true。

第二,为什么要使用四个布尔变量?相反,使用一个具有四种可能状态的变量。Enum对此很有好处:

代码语言:javascript
复制
enum Direction
{
    None = 0, // default
    North = 1,
    South = 2,
    East = 3,
    West = 4
}

用单向变量代替四个布尔值。变量越少,麻烦越少。

最后一次。我不太明白你怎么移动你的精灵?我是说,在改变和弦之前,你是如何探测方向的?在我看来,它应该这样做:

  1. 在Update()中,检测需要向哪个方向移动和更改方向变量,并在此基础上更改和更新sprite状态。
  2. 在当前状态下画精灵。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27278730

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档