【发布时间】:2016-05-01 12:19:01
【问题描述】:
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;
}
}
}
然而我的球没有移动:/ 停留在它的生成点
【问题讨论】:
-
您是否尝试过添加断点并单步执行您的代码?我会首先尝试,逐行逐行执行代码以确定
isRoundPlayin字段的状态,以及ballRect对象及其属性。也许您的X和Y属性正在更新,但是在设置坐标后是否需要在ballRect上调用方法或函数或某些东西?