Javascript 帆布游戏|如何控制射击速度?
目前我有代码每秒创建一个项目符号60次(setInterval) 我尝试过循环,但根本不起作用。 有人知道我如何调节火灾率吗Javascript 帆布游戏|如何控制射击速度?,javascript,html,canvas,Javascript,Html,Canvas,目前我有代码每秒创建一个项目符号60次(setInterval) 我尝试过循环,但根本不起作用。 有人知道我如何调节火灾率吗 谢谢。在大多数实时游戏中,你都会有一个主循环来控制你的动画。你可以在这上面加上消防装置 // 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 n
谢谢。在大多数实时游戏中,你都会有一个主循环来控制你的动画。你可以在这上面加上消防装置
// 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
}
欢迎光临!在这个网站上,你应该尝试自己编写代码。之后,如果你有问题,你可以张贴你已经尝试了一个明确的解释什么是不工作,并提供一个解决方案。我建议你读一个好的问题和答案。另外,请务必阅读。您已经是一个没有代码的低质量问题,请向我们展示您的code@AlonEitan是什么让这样一个问题得到这么多的选票而这样一个问题得到保护?“对我来说,这只是抽签的运气,这才是一个好问题。”Blindman 67很抱歉回答得太晚,但这个问题已经有7年多的历史了。时代变了,今天问同样的问题也会让你被否决和关闭。我认为这是一个在谷歌搜索中排名靠前的问题。不过我还是喜欢你的回答@盲人67和关于被保护的问题-阅读可能的原因