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 に答える