[英]Switch to button controlled game instead of keyboard
I'm building a simple browser game with html5 and java. 我正在使用html5和java构建一个简单的浏览器游戏。 Initially I managed controls of the game with the keyboard, now I'm trying to switch controls from keyboard to buttons, but nothing seems to work.
最初,我使用键盘来管理游戏的控件,现在我试图将控件从键盘切换为按钮,但是似乎没有任何效果。 I have no idea how to make the correct way.
我不知道如何做正确的方法。 Here's my code with keyboard controls:
这是我的键盘控制代码:
//Keyboard controls
document.onkeydown = function(e) {
var key = e.keyCode;
if (key == 37) {
dir = "left";
player.isMovingLeft = true;
} else if (key == 39) {
dir = "right";
player.isMovingRight = true;
}
if(key == 32) {
if(firstRun === true) {
init();
firstRun = false;
}
else
reset();
}
};
And my not working button code: 和我不工作的按钮代码:
document.onclick = function(e) {
var onclick = e.clickEvent;
if (onclick = "btn1") {
dir = "left";
player.isMovingLeft = true;
} else if (onclick = "btn2") {
dir = "right";
player.isMovingRight = true;
}
};
Thanks in advance, every help is appreciated! 在此先感谢您的帮助!
You need something like this: 您需要这样的东西:
document.onclick = function(e) {
var onclick = e.target.id; // e.target gets the element which originally triggered the event
if (onclick == "btn1") { // watch out for == instead of =
dir = "left";
player.isMovingLeft = true;
} else if (onclick == "btn2") {
dir = "right";
player.isMovingRight = true;
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.