1

我最近开始使用 Codecademy 学习 javascript。为了扩展我的知识并且只是为了好玩,我遵循了 Gyrostorm 的 HTML5 游戏教程:http ://www.youtube.com/playlist?list=PL290A4D2398C97186/ 。

现在我完成了制作我的游戏版本的教程,但我希望这样当你按住空格键时,子弹会以一定的速度发射。目前,您必须按住空格键才能开火。我有一个 Jet.shoot 函数和一个 Jet.checkShoot 函数,但是在多次尝试 setIntervals 和 setTimeouts 之后,没有任何效果!请帮忙!

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;
    }
}

这是我到目前为止的实时版本:https ://googledrive.com/host/0B2Xb6VzofFX-OGxEcnV3OUNTdFU/index.html

谢谢!

4

1 回答 1

0

如果你想有一个给定的射速,你需要处理好时间。所以最好的事情是有一个游戏时间,但无论如何你需要实现这样的解决方案(我在这个例子中使用了真实世界的时间(Date.now())):

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;
    }
}
于 2013-03-17T18:06:08.273 回答