Javascript 我如何才能创建一个恒定的子弹流?
我最近开始使用Codecademy学习javascript。为了扩展我的知识,只是为了好玩,我遵循了Gyrostorm的HTML5游戏教程: 现在我已经完成了制作我的游戏版本的教程,但我希望它能让你握住空格键时,子弹以一定的速度发射。目前,你必须一直按空格键才能开火。我有一个Jet.shoot功能和一个Jet.checkShoot功能,但在多次尝试设置间隔和设置超时后,没有任何效果!请帮忙Javascript 我如何才能创建一个恒定的子弹流?,javascript,html,web,Javascript,Html,Web,我最近开始使用Codecademy学习javascript。为了扩展我的知识,只是为了好玩,我遵循了Gyrostorm的HTML5游戏教程: 现在我已经完成了制作我的游戏版本的教程,但我希望它能让你握住空格键时,子弹以一定的速度发射。目前,你必须一直按空格键才能开火。我有一个Jet.shoot功能和一个Jet.checkShoot功能,但在多次尝试设置间隔和设置超时后,没有任何效果!请帮忙 Jet.prototype.checkShooting = function() { if(thi
Jet.prototype.checkShooting = function() {
if(this.isSpacebar && !this.isShooting) {
this.isShooting = true;
this.bullets[this.currentBullet].fire(this.noseX, this.noseY)
this.currentBullet++;
if(this.currentBullet >= this.bullets.length) {
this.currentBullet = 0;
}
} else if(!this.isSpacebar) {
this.isShooting = false;
}
};
Jet.prototype.shoot = function() {
this.isShooting = true;
this.bullets[this.currentBullet].fire(this.noseX, this.noseY);
this.currentBullet++;
if(this.currentBullet >= this.bullets.length) {
this.currentBullet = 0;
}
}
以下是我到目前为止的实况:
谢谢 如果你想要一个给定的火灾率,你需要处理时间。所以最好是有一个游戏时间,但无论如何,你需要实现这样一个解决方案,我使用了真实世界的时间日期。现在,对于这个例子:
if(this.isSpacebar && Date.now()>this.shootPossibleTime) {
this.shootPossibleTime =Date.now()+this.reloadTimeForThisWeapon;
this.bullets[this.currentBullet].fire(this.noseX, this.noseY)
this.currentBullet++;
if(this.currentBullet >= this.bullets.length) {
this.currentBullet = 0;
}
}
您应该使用onkeydown和onkeydup来激活/禁用拍摄function@llnk我在剧本的开头有这个。Onkeydown将isSpacebar设置为true,onkeyup将其设置为false。