【问题标题】:XNA spritesheet animationXNA spritesheet 动画
【发布时间】:2012-04-21 18:13:26
【问题描述】:

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

试图让我的角色在我朝不同方向移动时面对不同的方式。

问题是当我启动时我什至看不到我的纹理,奇怪的是当我走过游戏中的特定位置时,整个精灵表变得可见,我的 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);
    }

(忽略列表。添加)

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

【问题讨论】:

    标签: animation xna sprite-sheet


    【解决方案1】:

    这些行看起来没有必要

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

    您可能会看到 .Draw 调用中的最后一个参数。您现在将其设为 0f,即图层深度,如果该值低于您的地形,则地形将绘制在顶部……我认为,取决于您的 spritebatch.begin() 参数。您可以在此处阅读更多内容,其中解释了有关层深度的更多信息。 https://gamedev.stackexchange.com/questions/23619/2d-layerdepth-not-working-xna-4-0

    【讨论】:

    • 不幸的是,更改图层深度并没有帮助:/ 这两行使用向量移动矩形,因此非常需要它们 :)
    • 哪个矩形?我看到的唯一一个是 ObjectRectangle,它是一个源矩形,正确设置为ObjectRectangle = new Rectangle(CurrentFrame * FrameWidth, 0, FrameWidth, FrameHeight); 源矩形是相对于对象位置的。另外,您能否链接到您的 spritesheet 的图像,这可能有助于显示任何问题。
    • homepage.lnu.se/student/aw222fu/pics/CurrentSprites.png 就是这些,从上数第 5 个是字符 :D 另外,这意味着我可以完全删除这两行吗?
    • Spritesheet 看起来不错,但很难分辨,因为我在工作,看不太清楚。你能把代码上传到某个地方(或者一个仍然有错误的精简版本),我可以在它运行时查看它吗?
    • homepage.lnu.se/student/aw222fu/pics/PrototypeGame.rar 这应该就是全部了,也 - 注释掉上面的这两行修复了精灵但破坏了人工智能:/
    【解决方案2】:

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

    移动此代码

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

    【讨论】:

    • PositionX 和 PositionY 是 Position.X 和 Position.Y,我只是将它们设为自己的属性,以防我不得不限制其中一个。感谢您的移动提示 - 不幸的是我仍然看不到我的纹理:/
    • 啊,您不应该更改 ObjectRectangleX 或 Y。它们应该始终与您的第一个 new Rectangle(CurrentFrame * FrameWidth, 0, FrameWidth, FrameHeight); 相同。这就是精灵表的来源。试着把这两行注释掉。 ObjectRectangleX = (int)PositionX; ObjectRectangleY = (int)PositionY;
    • 好吧,这让精灵工作但完全破坏了人工智能(它正在移动怪物):P 所以我可以选择是否应该看到它们或者它们是否应该正确移动......: D
    • 为什么源矩形被用于 AI?您应该将您的位置用于 AI,而不是源矩形的位置。
    • 刚刚看到下面的代码。如果您注释掉 ObjectRectangleX/Y 代码并将 Monster.cs - Enemymovement(CharacterAnimation hero) 方法中的代码替换为 (int)Position.X/Y ,那么它应该可以工作。
    猜你喜欢
    • 2012-01-10
    • 2014-03-25
    • 1970-01-01
    • 2013-03-13
    • 2018-10-10
    • 2013-10-27
    • 2014-06-28
    • 2015-11-27
    • 2018-05-21
    相关资源
    最近更新 更多