2013-10-09 84 views
1

拍摄我正在做的太空射击游戏,我想知道如何使拍摄的延迟而不是空格键感谢:)如何添加延迟到按键

public void Shoot() 
    { 
     Bullets newBullet = new Bullets(Content.Load<Texture2D>("PlayerBullet")); 
     newBullet.velocity = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation)) * 5f; 
     newBullet.position = playerPosition + playerVelocity + newBullet.velocity * 5; 
     newBullet.isVisible = true; 

     if (bullets.Count() >= 0) 
      bullets.Add(newBullet); 
    } 

回答

0

你可以有滥发计数器,用于统计自上次拍摄以来的帧数。

int shootCounter = 0; 

...

if(shootCounter > 15) 
{ 
    Shoot(); 
    shootcounter = 0; 
} 
else 
{ 
    shootcounter++; 
} 

如果你想要去更高级的,你可以使用gameTime,默认的更新方法通常使您能够保持多少次因为你已经通过轨道”最后一枪。

+1

仅供参考:在XNA的'Update'方法是_supposedly_称为60次第二,但它有时滞后。更新调用与“Draw”方法的调用速率完全不同,所以这会比“帧”更“打勾”。但没有什么大不了的,只是说。 – gunr2171

+1

如果电脑速度更快的人可以更快速地拍摄,那么它就是一件大事:) –

4

我想你想使用XNA的GameTime属性。每当你射击一颗子弹时,你应该记录它发生的时间,作为当前的GameTime.ElapsedGameTime值。

这是TimeSpan所以你可以像下面比较一下:

// Create a readonly variable which specifies how often the user can shoot. Could be moved to a game settings area 
private static readonly TimeSpan ShootInterval = TimeSpan.FromSeconds(1); 

// Keep track of when the user last shot. Or null if they have never shot in the current session. 
private TimeSpan? lastBulletShot; 

protected override void Update(GameTime gameTime) 
{ 
    if (???) // input logic for detecting the spacebar goes here 
    { 
     // if we got here it means the user is trying to shoot 
     if (lastBulletShot == null || gameTime.ElapsedGameTime - (TimeSpan)lastBulletShot >= ShootInterval) 
     { 
      // Allow the user to shoot because he either has not shot before or it's been 1 second since the last shot. 
      Shoot(); 
     } 
    } 
} 
+0

更新了对逻辑的修复。 –

+0

谢谢:D我还没有修复它,但这已经教会了我的东西:) – Fwarkk