2017-05-13 47 views
-4

目前我有代碼創建一個子彈每秒60次(setInterval) 我試過循環,但根本沒有工作。 有沒有人有一個想法,我怎麼能調節火率?帆布遊戲|如何控制火率?

謝謝。

+2

歡迎來到[so]!在這個網站,你應該嘗試**自己編寫代碼**。後** [做更多的研究](//meta.stackoverflow.com/questions/261592)**如果你有問題,你可以**發佈你已經嘗試**與清楚的解釋是什麼是'工作**並提供[** Minimal,Complete和Verifiable示例**](// stackoverflow.com/help/mcve)。我建議閱讀[問]一個好問題和[完美問題](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/)。另外,一定要參加[遊覽]並閱讀[this](// meta.stackoverflow.com/questions/347937/)**。 –

+1

您已經[問](問)(http://stackoverflow.com/questions/43760692/html5-canvas-game-how-to-make-players-shoot)沒有代碼的低質量問題,向我們展示您的代碼 –

+1

@AlonEitan是什麼讓這樣的問題得到這麼多的選票被關閉,而像這樣的問題http://stackoverflow.com/q/2142535/3877726得到保護?對我來說,它只是平局的運氣,這是一個很好的問題。 – Blindman67

回答

2

在大多數實時遊戲中,你將有一個主循環來控制你的動畫。你會添加火力控制。

// object to hold details about the gun 
const gun = { 
    fireRate : 2, // in frames (if 60 frames a second 2 would be 30 times a second 
    nextShotIn : 0, // count down timer till next shot 
    update() { // call every frame 
     if(this.nextShotIn > 0){ 
      this.nextShotIn -= 1; 
     } 
    }, 
    fire(){ 
     if(this.nextShotIn === 0){ 
      // call function to fire a bullet 
      this.nextShotIn = this.fireRate; // set the countdown timer 
     } 
    } 
} 


function mainAnimationLoop() 
    // game code 


    gun.update(); 
    if(fireButtonDown){ 
     gun.fire(); // fire the gun. Will only fire at the max rate you set with fireRate 
    } 


    // rest of game code 
} 
+0

非常感謝!它工作得非常好! – Eden