2013-10-09 80 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