【问题标题】:How to make an old school Sprite movement like Pokemon (Tile to Tile)如何制作像 Pokemon 一样的老式 Sprite 运动(Tile to Tile)
【发布时间】:2014-02-23 11:02:04
【问题描述】:

我正在制作一个类似口袋妖怪的游戏,我需要让我的 Sprite 像在口袋妖怪的游戏中一样移动。 我的意思是,平铺到平铺,单元到单元。我正在使用 tIDE(一个平铺地图编辑器),我的平铺宽度是 32px。我希望玩家在移动过程中每 32px 移动 32px 并带有动画。和口袋妖怪里的一模一样。因此,如果我按住一个键,玩家会不断移动,如果我按一次键,他只会移动一次,所以 32px。

这是我目前的移动功能:

public void movePlayer(String keyDown, GameTime gameTime)
    {
        if (keyDown == "up")
        {
            playerPosition.Y -= 2;

            //Animation part, with a timer to switch animation
            if (time > 0)
            {
                directionSprite = directionSpriteTab[4];
                time -= gameTime.ElapsedGameTime.Milliseconds;
                time2 = interval;
            }
            if (time2 > 0 && time <= 0)
            {
                directionSprite = directionSpriteTab[5];
                time2 -= gameTime.ElapsedGameTime.Milliseconds;
            }
            if (time2 <= 0 && time <= 0)
            {
                time = interval;
            }
        }
        //same for other keys ...
        }

使用该代码,我的玩家可以顺利移动,但是当我停止按键时,他会停在两个瓷砖之间,碰撞真的很烦人,例如当我想进入一所房子时,门有 32px 大,很难制作玩家进入。

【问题讨论】:

    标签: xna sprite game-physics


    【解决方案1】:

    现在 Sprite 正常移动,但不是 Tile to Tile...如果我松开键,他会像以前一样移动,他会停止并且不会继续向方向 tile 移动。

    当我添加“向下”键部分时,它会做出奇怪的动作 x)

    这是带有“down”的代码,你能看看我是否做错了吗?

        public void movePlayer(String keyDown, GameTime gameTime)
        {
            if (keyDown == "up")
            {
                movingUp = true;
                // Could be type Vector2 or something else, depends on what playerPosition is.
                playerMoveDestination = new Vector2(playerPosition.X, playerPosition.Y - 32);
            }
    
            if (movingUp)
            {
                // Calculates the distance from the destination tile.
                // When the distance is small enough you want to snap to the tile to avoid 
                // overshooting. In this case the snap distance is 2 or less pixels because the 
                // character moves 2 pixels per frame.
                if (playerPosition.Y - playerMoveDestination.Y < 2)
                {
                    playerPosition.Y = playerMoveDestination.Y;
                    movingUp = false;
                }
                else
                {
                    playerPosition.Y -= 2;
                }
            }
    
            if (keyDown == "down")
            {
                movingDown = true;
                // Could be type Vector2 or something else, depends on what playerPosition is.
                playerMoveDestination = new Vector2(playerPosition.X, playerPosition.Y + 32);
            }
            if (movingDown)
            {
                // Calculates the distance from the destination tile.
                // When the distance is small enough you want to snap to the tile to avoid 
                // overshooting. In this case the snap distance is 2 or less pixels because the 
                // character moves 2 pixels per frame.
                if (playerPosition.Y + playerMoveDestination.Y < 2)
                {
                    playerPosition.Y = playerMoveDestination.Y;
                    movingDown = false;
                }
                else
                {
                    playerPosition.Y += 2;
                }
            }
        }
    

    我像这样初始化了“playerMoveDestination”和movingDown/Up bool:

        bool movingUp = false; // Set to true when keyDown == "up".
        bool movingDown = false;
    
        Vector2 playerMoveDestination;
    

    我把它们放在“movePlayer”函数上面。

    再次感谢您的帮助!

    【讨论】:

      【解决方案2】:

      您可以添加布尔变量或枚举来保存玩家运动的状态(枚举可能更好,但为简单起见,我将在示例中使用 bool)。例如

      bool movingUp = false; // Set to true when keyDown == "up".
      

      然后您可以使用 if 语句检查玩家是否位于下一个图块的坐标处。如果不是,请继续更新 playerPosition.Y。像这样的

       if (keyDown == "up")
       {
           movingUp = true;
           // Could be type Vector2 or something else, depends on what playerPosition is.
           playerMoveDestination = new Point(playerPosition.X, playerPosition.Y - 32);
       }
      
       if (movingUp)
       {
           // Calculates the distance from the destination tile.
           // When the distance is small enough you want to snap to the tile to avoid 
           // overshooting. In this case the snap distance is less than 2 pixels because the 
           // character moves 2 pixels per frame.
           if (playerPosition.Y - playerMoveDestination.Y < 2)
           {
               playerPosition.Y = playerMoveDestination.Y;
               movingUp = false;
           }
           else
           {
               playerPosition.Y -= 2;
      
               if (time > 0)
               {
                   directionSprite = directionSpriteTab[4];
                   time -= gameTime.ElapsedGameTime.Milliseconds;
                   time2 = interval;
               }
               if (time2 > 0 && time <= 0)
               {
                   directionSprite = directionSpriteTab[5];
                   time2 -= gameTime.ElapsedGameTime.Milliseconds;
               }
               if (time2 <= 0 && time <= 0)
               {
                   time = interval;
               }
           }
       }
      

      现在如果玩家放开向上键,它将继续移动角色直到到达下一个图块。

      编辑:对不起,我的代码有错误。当玩家正好在棋子上时, playerPosition.Y % 32 的计算结果为 0,小于 2,因此当按下向上键时,它会立即执行并停止移动。为了解决这个问题,你可以有一个 playerMoveDestination 变量,当玩家按下 Up 键时,它被设置为 (playerPosition.X, playerPosition.Y - 32)。为此,我更改了代码。

      【讨论】:

      • 更改了答案以修复错误。如果它对您有用,请接受答案。我们需要更多像 Pokemon 这样的游戏 :)
      • 求你的帮助,我不认为游戏会完成^^“这只是一个学校的项目。我回答你回答还有一些问题:P
      猜你喜欢
      • 2016-01-24
      • 1970-01-01
      • 1970-01-01
      • 2014-10-13
      • 1970-01-01
      • 1970-01-01
      • 2018-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多