【问题标题】:XNA 2D Camera - How to make it properly follow spriteXNA 2D 相机 - 如何使其正确跟随精灵
【发布时间】:2014-10-22 01:45:33
【问题描述】:

大家好!

让我们切入正题。 我制作了一个瓷砖引擎,可以相对于我的相机绘制我的地图(绘制的瓷砖是相机窗口中可见的瓷砖),并且我制作了一个位于相机中心的精灵(我的角色)。 每当我移动相机时,我的角色都会相应地跟随,唯一的问题是当我到达地图的边界时。我的编程方式限制了我的相机移动到边界之外,从而限制了我的角色移动到更靠近边界的位置(因为它总是在相机内居中)。

如何让我的相机摆脱邪恶地图的束缚?

附言我制作了一个picture 来说明我想要做什么。 这是我关注的guide

这是我绘制瓷砖并放置相机的代码:

protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.Black);

        spriteBatch.Begin();

        Vector2 firstSquare = new Vector2(Camera.Location.X / Tile.TileWidth, Camera.Location.Y / Tile.TileHeight);
        int firstX = (int)firstSquare.X;
        int firstY = (int)firstSquare.Y;

        Vector2 squareOffset = new Vector2(Camera.Location.X % Tile.TileWidth, Camera.Location.Y % Tile.TileHeight);
        int offsetX = (int)squareOffset.X;
        int offsetY = (int)squareOffset.Y;

        for (int y = 0; y < squaresDown; y++)
        {
            for (int x = 0; x < squaresAcross; x++)
            {
                foreach (int tileID in myMap.Rows[y + firstY].Columns[x + firstX].BaseTiles)
                {
                    spriteBatch.Draw(
                        Tile.TileSetTexture,
                        new Rectangle(
                            (x * Tile.TileWidth) - offsetX, (y * Tile.TileHeight) - offsetY,
                            Tile.TileWidth, Tile.TileHeight),
                        Tile.GetSourceRectangle(tileID),
                        Color.White);
                }
            }
        }

        spriteBatch.End();
        // TODO: Add your drawing code here

        base.Draw(gameTime);
    }

这是我移动相机的代码:

protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        IsMouseVisible = true;
        KeyboardState ks = Keyboard.GetState();
        if (ks.IsKeyDown(Keys.A))
        {
            Camera.Location.X = MathHelper.Clamp(Camera.Location.X - 2, 0, (myMap.MapWidth - squaresAcross) * Tile.TileWidth);
        }

        if (ks.IsKeyDown(Keys.D))
        {
            Camera.Location.X = MathHelper.Clamp(Camera.Location.X + 2, 0, (myMap.MapWidth - squaresAcross) * Tile.TileWidth);
        }

        if (ks.IsKeyDown(Keys.W))
        {
            Camera.Location.Y = MathHelper.Clamp(Camera.Location.Y - 2, 0, (myMap.MapHeight - squaresDown) * Tile.TileHeight);
        }

        if (ks.IsKeyDown(Keys.S))
        {
            Camera.Location.Y = MathHelper.Clamp(Camera.Location.Y + 2, 0, (myMap.MapHeight - squaresDown) * Tile.TileHeight);
        }
        // TODO: Add your update logic here

        base.Update(gameTime);
    }

【问题讨论】:

  • 当相机到达边缘时,然后移动角色。或在您捕捉到按键的位置添加一些偏移量(例如:myMap.MapWidth - squaresAcross + offset),以便相机移动得更远。

标签: c# camera xna 2d sprite


【解决方案1】:

您需要让您的相机跟随玩家,而不是让您的玩家跟随相机。这样你就可以对相机设置限制,而不是试图破解相机系统来让角色做事。

每次更新时,您都会有类似的内容:

Player.Update(gametime);
Camera.Update(Player.Position);

【讨论】:

  • 在尝试您的解决方案时,我注意到我的程序存在另一个缺陷。每当我的相机移出边界时,程序就会尝试绘制具有负索引的图块,从而导致游戏崩溃。我可以在这里获得帮助,还是应该发布一个新问题?
  • 发生这种情况是因为当您获得负相机坐标时,您正在索引一个负值。您假设您的地图始终从 0,0 开始,因此解决此问题的最简单方法是根本不处理负坐标。像这样初始化: int firstY = (int)MathHelper.Clamp(firstSquare.Y, 0, squaresDown); int firstX = (int)MathHelper.Clamp(firstSquare.X, 0, squaresAcross);
  • 感谢到目前为止的帮助,我真的很感激。但是即使在应用了你的代码之后,我的程序仍然会在我脸上打一个error code。我发现有些东西仍然给它一个负值,所以我尝试添加 this 代码,最终证明完全没有帮助。
  • 错误代码的翻译是:索引超出范围。必须为非负数且小于集合参数名称的大小
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-03
相关资源
最近更新 更多