XNA spritesheet动画

时间:2012-04-09 10:44:51

标签: animation xna sprite-sheet

首次使用XNA创建一个简单的RPG游戏。

每当我向不同的方向移动时,试图让我的角色面对不同的方式。

问题在于,当我开始时我甚至看不到我的纹理,奇怪的是当我走过游戏中的特定位置时整个spritesheet变得可见,对于我的AI NPC也是如此:s ..它是就像他们躺在背后,我正在移动的矩形是透明的(所以我可以通过背景看到它)。

当我创建类的实例时,会发送FrameWidth和FrameHeight。 (height = 0(从spritesheet的顶部开始),width = spritesheetwidth / 4(将单个sprite输出)。)

速度是角色移动的速度。

    public override void Update(GameTime gameTime)
    {
        ObjectRectangle = new Rectangle(CurrentFrame * FrameWidth, 0, FrameWidth, FrameHeight);
        Origin = new Vector2(ObjectRectangle.Width / 2, ObjectRectangle.Height / 2);
        Position += Velocity;

        ObjectRectangleX = (int)PositionX;
        ObjectRectangleY = (int)PositionY;

        #region movement
        if (Keyboard.GetState().IsKeyDown(Keys.D) && Pastkey.IsKeyUp(Keys.A) && Pastkey.IsKeyUp(Keys.W) && Pastkey.IsKeyUp(Keys.S))
        {
            AnimateRight(gameTime);
            VelocityX = 2;
        }
        else if (Keyboard.GetState().IsKeyDown(Keys.A) && Pastkey.IsKeyUp(Keys.D) && Pastkey.IsKeyUp(Keys.W) && Pastkey.IsKeyUp(Keys.S))
        {
            AnimateLeft(gameTime);
            VelocityX = -2;
        }
        else if (Keyboard.GetState().IsKeyDown(Keys.W) && Pastkey.IsKeyUp(Keys.A) && Pastkey.IsKeyUp(Keys.D) && Pastkey.IsKeyUp(Keys.S))
        {
            AnimateUp(gameTime);
            VelocityY = -2;
        }
        else if (Keyboard.GetState().IsKeyDown(Keys.S) && Pastkey.IsKeyUp(Keys.A) && Pastkey.IsKeyUp(Keys.W) && Pastkey.IsKeyUp(Keys.D))
        {
            AnimateDown(gameTime);
            VelocityY = 2;
        }
        else
        {
            Velocity = Vector2.Zero;
        }

        Pastkey = Keyboard.GetState();

        #endregion
    }

    #region Animations

    public void AnimateDown(GameTime gameTime)
    {
        CurrentFrame = 2;
    }
    public void AnimateRight(GameTime gameTime)
    {
        CurrentFrame = 3;
    }
    public void AnimateLeft(GameTime gameTime)
    {
        CurrentFrame = 1;
    }
    public void AnimateUp(GameTime gameTime)
    {
        CurrentFrame = 0;
    }

    #endregion

}

Draw方法如下:

    public virtual void Draw(SpriteBatch spriteBatch)
    {

        spriteBatch.Draw(ObjectTexture, Position, ObjectRectangle, Color.White, 0f, Origin, 1.0f, SpriteEffects.None, 0f);
        GameList.Add(this);
    }

(忽略List.Add)

所以我需要帮助的是将纹理“锁定”到矩形。

2 个答案:

答案 0 :(得分:1)

这些行看起来不必要

ObjectRectangleX = (int)PositionX;
ObjectRectangleY = (int)PositionY;

您可能会看到的一件事是.Draw调用中的最后一个参数。你现在把它当作0f,即图层深度,如果该值低于你的地形,那么地形将被绘制在顶部......我认为,取决于你的spritebatch.begin()参数。您可以在这里阅读更多内容,其中有关于层深度的更多位。 https://gamedev.stackexchange.com/questions/23619/2d-layerdepth-not-working-xna-4-0

答案 1 :(得分:0)

我最初的想法是你在知道帧数据之前计算这些值。

移动此代码

ObjectRectangle = new Rectangle(CurrentFrame * FrameWidth, 0, FrameWidth, FrameHeight);
Origin = new Vector2(ObjectRectangle.Width / 2, ObjectRectangle.Height / 2);
Position += Velocity;

ObjectRectangleX = (int)PositionX;
ObjectRectangleY = (int)PositionY;

更新方法的底部会为您提供更好的结果。此外,它看起来不像您正在更新PositionX或PositionY。您应该使用Position.X和Position.Y。

编辑 - 您正在更改源图像的位置。因此,当您在屏幕上移动时,您的位置会更新,将源矩形移动到更接近应有的位置。 因此,只需注释掉以下两行即可解决问题。

ObjectRectangleX = (int)PositionX;
ObjectRectangleY = (int)PositionY;

编辑2 - 澄清

要修复源矩形,并保持AI工作,您需要执行以下操作。 在CharacterAnimation.cs和Monster.cs的Update方法中,注释掉两行

ObjectRectangleX = (int)PositionX;
ObjectRectangleY = (int)PositionY;

然后在Monster.cs中的Enemymovement(CharacterAnimation hero)方法修改这些行(45,46)

ObjectcentreX = ObjectRectangle.X + (ObjectTexture.Width / 2);
ObjectcentreY = ObjectRectangle.Y + (ObjectTexture.Height / 2);

成为

ObjectcentreX = (int)PositionX + (ObjectTexture.Width / 2);
ObjectcentreY = (int)PositionY + (ObjectTexture.Height / 2);
相关问题