2017-12-28 18 views
1

私と同様の質問がありますが、私の場合はうまくいかず、もっと時間を無駄にすることはできません。JS Game - 定義されていない型エラーのプロパティを設定できません

私はJSを学んでいるので、簡単なパックマンゲームをコーディングしようとしています。この場合、パクマンがパワーアップを食べるたびに、すべてのアクティブな幽霊は弱い幽霊に変身しなければなりません。

Uncaught TypeError: Cannot set property 'isWeak' of undefined. 

を、私はこの質問にすべてのコードを投稿できませんでした:

の問題は、私がプレイしていると私はパワーアップをつかむながら、ゲームが言ってクラッシュしたときに付属しています。エラーが発生した主な部分のみ(私はそうだと思う)。

main.jsでの私のコード:ghost.jsで

var activeGhosts = []; 
var powerups = []; 
for (var i = 0; i < powerups.length; i++) { 
    if (pacman.collision(powerups[i])) { 
     makeWeak(); 
     powerups.splice(i,1); 
    } 

} 
function makeWeak() { 
    for (var i = 0; i < activeGhosts.length; i++) activeGhosts[i].isWeak = true; 
} 

マイコード:

function Ghost(x,y,img){ 
this.x = x; 
this.y = y; 
this.img = img; 
this.direction = 0; 
this.radius = 16; // half of 32 px because every image is 32x32 px 
this.crash = false; 
this.isWeak = false; 

this.show = function() { 
    if (this.isWeak) { 
     image(weakghostimg, this.x, this.y); 
    } else { 
     // img can be the all the different ghosts 
     image(img, this.x, this.y); 
    } 
}; 
+1

どのように新しい幽霊を作成し、activeGhost配列にそれらを入れていますか? – Andy

+0

私は関数間のリンクを見ません。また、エラーを投げている行を表示してください – brk

+0

私は[新しいゴーストのインスタンスを追加するとあなたのコードが動作するので](https://jsfiddle.net/0rkjLgLo/) 。 – Andy

答えて

0

私は別のシンボルと23x22の列であるプラットフォームを持っています。私が「再」を検出すると、その位置に赤い幽霊を作らなければならないということです。

var ghosts = []; // array to draw them 
var activeGhosts = []; // array to keep them alive and moving around 

for (var i = 0; i < plat.rows; i++) { 
    for (var j = 0; j < plat.columns; j++) { 
    if (plat.platform[i][j] === 're') ghosts.push(new Ghost(j * 32, i * 32, redGhostimg)); 

これは、ゴーストをactiveGhostsアレイに挿入するために使用する機能です。ゴーストの動きを開始するには、2秒ごとに1つのゴーストを別の配列に移動します。

function ghostsEscape() { 
if (ghosts.length > 0) { 
    var tempGhost = ghosts.pop(); 
    tempGhost.escape(plat); 
    activeGhosts.push(tempGhost); 
} else if (ghosts.length === 0) return; 
setTimeout(ghostsEscape, 2000); 

}

関連する問題