1

第一次使用 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

}

绘制方法如下所示:

    public virtual void Draw(SpriteBatch spriteBatch)
    {

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

(忽略列表。添加)

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

4

2 回答 2

1

这些线条看起来没有必要

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

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

于 2012-04-09T11:31:02.620 回答
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);
于 2012-04-09T14:06:16.077 回答