我正在通过雄辩的Java语言工作。我在第15章“平台游戏”的结尾处,并且在弄清楚如何使游戏暂停时遇到了麻烦。
本章末尾的练习之一要求您执行此操作:“通过按Esc键可以暂停(暂停)和取消暂停游戏。
这可以通过将runLevel函数更改为使用另一个键盘事件处理程序,并在每次按Esc键时中断或恢复动画来完成。”作者说,可以通过重新排列runLevel调用runAnimation的方式来做到这一点。 :
function runLevel(level, Display, andThen) {
var display = new Display(document.body, level);
runAnimation(function(step) {
level.animate(step, arrows);
display.drawFrame(step);
if (level.isFinished()) {
display.clear();
if (andThen)
andThen(level.status);
return false;
}
});
}
我这样修改了功能:
function runLevel(level, Display, andThen) {
var display = new Display(document.body, level);
var paused = false;
addEventListener("keydown", function(event){
if (event.keyCode == 27) {
paused = !paused;
}
});
if (!paused){
runAnimation(function(step) {
level.animate(step, arrows);
display.drawFrame(step);
if (level.isFinished()) {
display.clear();
if (andThen)
andThen(level.status);
return false;
}
});
}
}
但是即使在我逃脱后,游戏仍继续进行。我不明白为什么。
您可以在以下位置找到游戏的完整代码:http : //eloquentjavascript.net/code/chapter/15_game.js在此先感谢您的帮助!
尝试将if语句放入回调函数中。
runAnimation(function(step) {
if (!paused){
level.animate(step, arrows);
display.drawFrame(step);
if (level.isFinished()) {
display.clear();
if (andThen)
andThen(level.status);
return false;
}
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句