在游戏 JavaScript 中添加暂停键
Add pause key in a game javascript
我创建了一个小游戏,但我在放置暂停键时遇到了一些麻烦。它不起作用。你能看出哪里是错的并纠正我吗?谢谢。我想知道如何实现暂停键和重新启动键,以便不刷新页面。
游戏链接法典:
//this is where the keybinding occurs
$(document).keydown(function(e){
if(!gameOver && !playerHit){
if (!gamePaused) {
game = clearTimeout(game);
gamePaused = true;
} else if (gamePaused) {
game = setTimeout(gameLoop, 1000 / 30);
gamePaused = false;
}
按下键:
switch(e.keyCode){
case 75: //this is shoot (k)
//shoot missile here
var playerposx = $("#player").x();
var playerposy = $("#player").y();
var name = "playerMissle_"+Math.ceil(Math.random()*1000);
$("#playerMissileLayer").addSprite(name,{animation: missile["player"], posx: playerposx + 90, posy: playerposy + 14, width: 36,height: 10});
$("#"+name).addClass("playerMissiles")
break;
case 65: //this is left! (a)
$("#playerBooster").setAnimation();
break;
case 87: //this is up! (w)
$("#playerBoostUp").setAnimation(playerAnimation["up"]);
break;
case 68: //this is right (d)
$("#playerBooster").setAnimation(playerAnimation["booster"]);
break;
case 83: //this is down! (s)
$("#playerBoostDown").setAnimation(playerAnimation["down"]);
break;
暂停键 P:
case 80: //pause (p)
pauseGame();
alert ("paused")
}
}
});
密钥发布:
//this is where the keybinding occurs
$(document).keyup(function(e){
if(!gameOver && !playerHit){
if (!gamePaused) {
game = clearTimeout(game);
gamePaused = true;
} else if (gamePaused) {
game = setTimeout(gameLoop, 1000 / 30);
gamePaused = false;
switch(e.keyCode){
case 65: //this is left! (a)
$("#playerBooster").setAnimation(playerAnimation["boost"]);
break;
case 87: //this is up! (w)
$("#playerBoostUp").setAnimation();
break;
case 68: //this is right (d)
$("#playerBooster").setAnimation(playerAnimation["boost"]);
break;
case 83: //this is down! (s)
$("#playerBoostDown").setAnimation();
break;
case 80: //pause (p)
pauseGame();
}
}
});
暂停功能:
function Pause () {
if (!gamePaused) {
game = clearTimeout(game);
gamePaused = true;
} else if (gamePaused) {
game = setTimeout(gameLoop, 1000 / 30);
gamePaused = false;
}};
尝试检查 P 键是否已注册(使用警报或控制台消息)。然后
我还建议如下:不要在 keyUp/keyDown 处理程序中设置实际的游戏循环间隔(或调用游戏函数)。将实际的游戏代码放在它们自己的函数中,例如:
bGamePaused = false;
// key handlers
$(document).keydown(function(e){
switch(e.keyCode){
case 80:
// check if the key is registered
console.log("p is pressed, pause the game!");
// toggle the paused status:
bGamePaused = !bGamePaused;
// tell gameQuery to pause or resume the game
(bGamePaused) ? pauseGame() : resumeGame();
break;
}
$(document).keyup(function(e){
switch(e.keyCode){
// do not check for pause here. you only need to check when the key is either pressed or released, or the function will get called twice.
}
相关文章:
- 我可以向数组添加对象键吗
- 如何在 if else 语句期间添加暂停/延迟
- tinyMCE:如何添加快捷键Ctrl + Enter
- 在对象数组中添加匹配键的值
- 添加重复键对象时如何更新 ng-repeat 列表中的元素
- 如何防止向 JavaScript 数组添加重复键
- 如何运行新窗口.打开并自动添加 Ctrl 键
- j查询添加 在键事件 Enter 上,使用 将其发送到后端
- 在javascript游戏中添加暂停函数时遇到问题
- 如何在wowsliderjs文件中添加暂停和播放按钮
- 如何向正在运行的setInterval添加暂停/播放功能
- 为滑块添加暂停功能
- 在javascript中添加子键值对到现有数组(或JSON对象?)
- 为AJAX Live Search PHP添加快捷键(键盘箭头向上/向下)支持
- 当用户从移动通知控件中按下播放/暂停键时,我如何重写默认操作
- 如何将为每个对象添加一个键的每个函数转换为Ramda
- Django-逆向工程管理网站's”;添加外键”;按钮
- 使用 JS 向 html 元素添加快捷键
- 存储在变量中的JSON添加了键
- 在游戏 JavaScript 中添加暂停键