2011-12-19 8 views
0

私は複数のゲームとして同時に実行できるメモリゲームを作成しています。ゲームを再起動したり、新しいゲームを実行した場合に残る最高のスコア(最高得点)を保存することは可能です。異なるインスタンスからの値へのアクセス

問題が発生しました。変数 "bestScore"を "this.bestScore"として保存すると、特定のゲームをポイントします。つまり、ゲームを再起動するとリセットされます。代わりに "var"を使用しようとしましたが、 "DESKTOP.MemoryApp.bestScore"(下のコードを参照)でアクセスしようとすると、定義されていません。

したがって、この値をすべてのゲームでアクセスできるように保存するにはどうすればよいですか?

DESKTOP.MemoryApp = function(){ 

this.score = 0; 
this.bestScore = 0; 
var bestScore = 0; 

} 

DESKTOP.MemoryApp.prototype.wonGame = function(){ 

// code... 

console.log(this.bestScore) // <-- points to a specific game 
console.log(DESKTOP.MemoryApp.bestScore // <-- undefined 
console.log(bestScore) <-- bestScore is not defined 

}

答えて

1

ストアそれあなたは、後者の場合にはそれにアクセスしようとしているよう:

// this will work from the "constructor" function as well 
DESKTOP.MemoryApp.bestScore = 100; 
console.log(DESKTOP.MemoryApp.bestScore); // of course, 100 
+0

ああ、もちろん!ありがとう! – holyredbeard

関連する問題