我有一个检查游戏是否结束的功能,我想延迟一下,以便让用户有时间在通过屏幕查看游戏之前先查看每个答案是否正确。
在没有超时的情况下运行游戏时,它的检查工作正常,但是当我设置超时时,我得到一个错误,即函数调用不是函数。
setTimeout(function() {this.CheckGameOver();}, (5 * 1000));
设置超时的方式有问题吗?这是唯一的想法,因为该功能本身可以完美运行。
CheckGameOver: function() {
// Check game over clause
var c = this.View.children;
if (this.players[0].answered == 13) { // Check if Player 1 won
// Win the game
gameStopped = true;
isFirstQuestionSetup = true;
if(this.PlayerCount == 1) {
views.get('singlePlayerGame').transitionOut();
clearInterval(this.cpu);
} else {
views.get('twoPlayerGame').transitionOut();
}
views.get('background').switchState(BACKGROUND_STATES.GAME_OVER);
views.get('genericHud').switchState(HUD_STATES.MAIN_MENU);
views.get('gameOver').transitionIn(this.players, this.GameType, this.Language);
}
if (this.playerCount == 1) {
if (this.cpuAnswered == 13) { // Check if CPU won
// Lose the Game (Doh!)
gameStopped = true;
views.get('singlePlayerGame').transitionOut();
clearInterval(this.cpu);
views.get('background').switchState(BACKGROUND_STATES.GAME_OVER);
views.get('genericHud').switchState(HUD_STATES.MAIN_MENU);
views.get('gameOver').transitionIn(this.players, this.GameType, this.Language);
}
} else {
if (this.players[0].answered == 13) { // Check if Player 2 won
// Player 2 Wins
gameStopped = true;
views.get('twoPlayerGame').transitionOut();
views.get('background').switchState(BACKGROUND_STATES.GAME_OVER);
views.get('genericHud').switchState(HUD_STATES.MAIN_MENU);
views.get('gameOver').transitionIn(this.players, this.GameType, this.Language);
}
}
},
this
setTimeout函数中的内容与您认为的无关。您可以使用es6箭头功能
setTimeout(() => {this.CheckGameOver();}, (5 * 1000));
或绑定this
到setTimeout
setTimeout(function() {this.CheckGameOver();}.bind(this), (5 * 1000));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句