0

だから私は、主に小惑星のコピーである、ばかげた小さなキャンバスゲームを書いています。とにかく、ユーザーがスペースバーを押すと、プレーヤーオブジェクトのfire()関数が呼び出されるように、ボタンリスナーを設定しました。

eGi.prototype.keyDownListener = function(event) {
    switch(event.keyCode) {
        case 32:
            player.fire();
            break;

fire関数では、スクリプトがプロセスがすでに実行されているかどうかを確認し、実行されていない場合は、新しい「bullet」オブジェクトを作成して一時変数に格納し、描画スタックに追加します。

fire:(function() {
    if (this.notFiring) {
        var blankObject = new bullet(this.x,this.y,this.rot,"bullet");
        objects.push(blankObject);
        timer2 = setTimeout((function() {
            objects.pop();
        }),1000);
        this.notFiring = false;
    }}),

(ちなみに、ユーザーがスペースバーを離すと、this.notFiringtrueに戻ります。)

これは、弾丸オブジェクトのコンストラクターと、その必須およびプロトタイプのメソッドですdraw(context)

var bullet = function(x,y,rot,name) {
    this.x = x;
    this.y = y;
    this.sx = 0;
    this.sy = 0;
    this.speed = 1;
    this.maxSpeed = 10;
    this.rot = rot;
    this.life = 1;
    this.sprite = b_sprite;
    this.name = name;
}
bullet.prototype.draw = function(context) {
    this.sx += this.speed * Math.sin(toRadians(this.rot));
    this.sy += this.speed * Math.cos(toRadians(this.rot));
    this.x += this.sx;
    this.y -= this.sy;
    var cSpeed = Math.sqrt((this.sx*this.sx) + (this.sy * this.sy));
    if (cSpeed > this.maxSpeed) {
        this.sx *= this.maxSpeed/cSpeed;
        this.sy *= this.maxSpeed/cSpeed;    
    }
    context.drawImage(this.sprite,this.x,this.y);
}

とにかく、ゲームを実行してスペースバーを押すと、Chromeデベロッパーコンソールに次のようなエラーが表示されます。

Uncaught TypeError: Object function (x,y,rot,name) {
    this.x = x;
    this.y = y;
    this.sx = 0;
    this.sy = 0;
    this.speed = 1;
    this.maxSpeed = 10;
    this.rot = rot;
    this.life = 1;
    this.sprite = b_sprite;
    this.name = name;
} has no method 'draw'

プロトタイプを作ったのに。私は何が間違っているのですか?

編集:

に変更var bullet = functionしてfunction bulletに変更bullet.prototype.drawした後bullet.drawも、エラーが発生します。今回はもっと不思議だと言って

Uncaught TypeError: type error
    bullet.draw
    (anonymous function)
    eGi.drawObjs
    eGi.cycle

完全なコードは私のウェブサイト、ここにあります

別の編集:

Chromeコンソールは、このタイプのエラーが122行目で発生していることを示しています。これは、たまたまコードのスニペットです。

context.drawImage(this.sprite,this.x,this.y);

ただし、そこでタイプエラーが発生する可能性があるかどうかはわかりません。スプライトは画像であり、X値とY値は未定義ではなく、数値です。

4

1 に答える 1

1

描画関数をどこで呼び出していますか?bullet.draw();実際の弾丸インスタンスで呼び出すのではなく、呼び出しているに違いありません。

ちょっと違いが好き

Cat.meow();

var mittens = new Cat();
mittens.meow();
于 2012-07-25T04:54:29.467 に答える