【问题标题】:Remembering a direction, moving to a point, then changing direction. XNA记住一个方向,移动到一个点,然后改变方向。 XNA
【发布时间】:2015-04-24 00:32:03
【问题描述】:

我正在 XNA 中制作一个 pacman 克隆。 到目前为止,我已经使用 2D 数组绘制了瓦片地图,使用另一个 2D 数组添加了药丸,并制作了一个允许 pacman 移动的 2D 数组。

在实际游戏中,你可以在向上移动的同时按右,它会等到你能够向右移动和转弯。

我有一个系统,仅当 spritePosition % 32 = 16 时才允许转弯。 这意味着精灵将在墙壁之间居中。

我需要程序记住最后按下的键或在转弯前移动到正确的位置,但我找不到这样做的方法。 这是一些涵盖我正在尝试的代码。

public void MovementCheck()
        {
            presentKey = Keyboard.GetState();
            spritePosition = spriteVelocity + spritePosition;
            pacManRec = new Rectangle((int)spritePosition.X,     (int)spritePosition.Y, pacManTex.Width, pacManTex.Height);
            spriteOrigin = new Vector2(pacManRec.Width / 2, pacManRec.Height / 2);

        //Press Right
        if (presentKey.IsKeyDown(Keys.Right) && pastKey.IsKeyUp(Keys.Right))
        {

            Right();
        }
}

private void Right()
        {
            direction = "right"; 
//if the next block in tile map to the right is a 1, and the sprite is centred - allow a turn
            if (inputMap[(int)Math.Floor(spritePosition.Y / 32), (int)Math.Floor(spritePosition.X / 32) + 1] == 1 && (spritePosition.Y % 32 == 16))
            {
                rotation = ((float)Math.PI / 180);
                spriteVelocity.X = 0;
                spriteVelocity.Y = 0;
                spriteVelocity.X = movementSpeed;
            }
        }

只显示了右键,其他类似,但方向都改变了,对瓷砖地图的检查也相应改变。 (这里的 X 上 +1)

我试过了

while (spritePosition.Y % 32 != 16)
{ spritePosition = spriteVelocity + spritePosition; }

但这只会让精灵弹到屏幕上,(很明显):(

我在 Right() 调用之前尝试了一个新方法

bool RightCheck()
{
    if ( CONDITIONS MET HERE )
    return true
    else
    { 
      //dont remember if I used this line, but something similar
      spritePosition = spriteVelocity + spritePosition;
      RightCheck()
    }
    return false; //allows program to build
 }

只是一个导致无限递归。

【问题讨论】:

    标签: c# xna game-physics pacman


    【解决方案1】:

    一种解决方案是添加一个int counter = 0;,您可以在每帧/时间步中更新您的游戏循环(使用counter++;)。每次进行有效输入并保存该输入时设置为 0。

    概述代码:

    public class GameClassWhereUpdateIsDone
    {
        private enum Directions { None, Up, Down, Left, Right };
    
        private int counter = 0;
        private Directions currentDirection; // Current movement-direction
        private Directions lastInput; // Last direction from input
    
        public void Update(...)
        {
           var keyboardState = Keyboard.GetState();
           if(keyboardState.IsKeyPressed(Keys.Right))
           {
               counter = 0;
               direction = Directions.Right;
           }
    
           if(currentDirection != lastInput && counter < 5) // Allow turning 5 updates ahead.
           {
               // Player want to turn
               if(AllowedToTurn(lastInput)
               {
                    currentDirection = lastInput;
               }
           }
    
           MoveDirection(currentDirection);
    
           counter++;
        }
    
        private bool AllowedToTurn(Directions direction)
        {
           if(direction == Directions.Right)
           {
               return RightCheck();
           }
        }
    }
    

    关键思想是跟踪移动方向和输入的最后一个方向......

    在最初的 Pac-Man 中实际使用了“预转弯”,这意味着如果您根据以下公式在拐角前转弯,您将开始沿对角线移动:http://home.comcast.net/~jpittman2/pacman/pacmandossier.html,这很有趣。

    【讨论】:

    • 感谢您的回答,我刚刚重新检查了此页面。我已经看过 PacMan 档案,而且读起来很有趣。我会试一试,看看结果如何。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    相关资源
    最近更新 更多