1

こんにちは、JavaScript で問題が発生しています。以前の投稿をいくつか見ましたが、まだ答えが見つかりません。

私は Emotion-Object を持っている HTML5+Javascript ゲームに取り組んでいます。emotionImg は Image() で、タイプは String です。

function Emotion(emotionImg, type) {

this.emotionImg = emotionImg;
this.emotionImg.onload = function() {
    console.log("added emotion: "+type);
};
this.type = type;

// last emotion of a scene
this.isLast = false;

this.setLast = function() {
    this.isLast = true;
};

return this;

}

これらの感情は配列 (all_emotions) に格納されます。私のゲームでは感情がランダムに選択され、画像がキャンバスに描画されます。エモタイプは文字列です。

currEmotion1 = randomEmotion(emotype);
// set image
emo1img = currEmotion1.emotionImg;

私のランダム関数:

function randomEmotion(type) {
if(type == "happy") {
    return all_emotions[0][Math.floor(Math.random()*all_emotions[0].length)];
}
else if(type == "sad") {
    return all_emotions[1][Math.floor(Math.random()*all_emotions[0].length)];
}

ランダム関数を呼び出すと、次のエラーが表示されることがあります。

TypeError: currEmotion1 は定義されていません [Bei diesem Fehler anhalten]

emo1img = currEmotion1.emotionImg;

誰でもそれで私を助けることができますか?

4

1 に答える 1

0

randomEmotion関数内で がtypeの場合sad、 に基づいてランダム インデックスを生成していますが、all_emotions[0]から読み取りall_emotions[1]ます。それを以下の抜粋に変更すると、エラーが解決されます。

function randomEmotion(type) {
  if(type == "happy") {
    return all_emotions[0][Math.floor(Math.random()*all_emotions[0].length)];
  }
  else if(type == "sad") {
    return all_emotions[1][Math.floor(Math.random()*all_emotions[1].length)];
  }
}
于 2013-01-16T09:19:28.603 に答える