我试图定义在连续射击过程中两次射击之间的时间间隔,但通过以下尝试获得了奇怪的行为:
public void Shoot(GameTime time)
{
bullets.Add(new Bullet("bullet", position, angle, content, this, bullets) );
shotTimer = time.TotalGameTime.Milliseconds;
}
public void ShootContinuous(GameTime time)
{
if (time.TotalGameTime.Milliseconds - shotTimer > 50)
this.Shoot(time);
}上面的代码是这样调用的:
if (newMouseState.LeftButton == ButtonState.Pressed)
{
if (oldMouseState.LeftButton == ButtonState.Released)
{
player.Shoot(time);
gui.ProcessClick(newMouseState);
}
else
player.ShootContinuous(time);
}好吧,它的行为是这样的:当按住按钮的时候,它会随机发射一发子弹数在4-10之间的抽射,然后什么也不做,直到我重新按下按钮,等待片刻,然后再发射一次。
有人知道这有什么问题吗?
发布于 2012-05-25 17:45:28
如果我正确理解了代码,您应该使用TotalMilliseconds,而不是Milliseconds。
https://stackoverflow.com/questions/10752061
复制相似问题