XNA 球运动

XNA ball movement

XNA 相关:

我需要帮助解决这个小问题,所以我想首先获得一个从 1 到 4 的随机数,然后根据该数字的值,球将移动 ( 1(forward,up ), 2(向后,向上), 3(向前,向下), 4(向后,向下)).

在我的代码中,我正在检查当前是否正在玩游戏,以便球只能在回合开始时获得它的方向。 (如果玩家获胜或死亡,isRoundPlayin 设置为 false,当球获得随机方向(1、2、3 或 4)时,它设置为 true。

bool isRoundPlayin = false; // this is at the top of my Game1.cs 

//...
//...
//...
// This is an Update method in Game1.cs:

    protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

        if (isRoundPlayin == false)
        {
            BallMove();
            isRoundPlayin = true;
        }

        base.Update(gameTime);
    }
//...
//...
//...

// This is BallMove method at the end of Game1.cs script:
 public void BallMove()
        {
            Random randy = new Random();
            int randMov = randy.Next(1, 4);
            int ball_velocity = 3;
            {
                if (randMov == 1)
                {
                    ballRect.X += ball_velocity;
                    ballRect.Y += ball_velocity;
                }
                else if (randMov == 2)
                {
                    ballRect.X += ball_velocity;
                    ballRect.Y -= ball_velocity;
                }
                else if (randMov == 3)
                {
                    ballRect.X -= ball_velocity;
                    ballRect.Y += ball_velocity;
                }
                else if (randMov == 4)
                {
                    ballRect.X -= ball_velocity;
                    ballRect.Y -= ball_velocity;
                }
            } 
        }

然而我的球没有移动:/ 停留在它的重生点

这是因为您设置的是在移动后立即播放 true。因此它移动一帧然后不再更新。我不确定为什么您希望球移动一帧时结束回合。如果您向我们提供了更多关于您的需求的信息,我们也许可以帮助您,但目前,只需从 if 语句中删除 isRoundPlaying = true 即可。