我是javascript的新手,我正在尝试制作一个迷宫游戏,我用箭头和wasd移动,但我无法弄清楚如何移动触摸设备的html按钮,这是我的js代码:
function moveRect(e) {
var estado = new Boolean(false); //variable to know if the player won or not
var pos = []; //saves every position player moves to
var newX;
var newY;
var canMove; // i'm not using this right now
e = e || window.event;
moveA = 22; //1
moveB = 22; //22
var angle = 0;
switch (e.keyCode) {
case 38: // upkey
newX = currRectX;
newY = currRectY - moveA;
angle = 90;
break;
case 87: // W key
newX = currRectX;
newY = currRectY - moveB;
angle = 90;
break;
case 37: // leftkey
newX = currRectX - moveA;
newY = currRectY;
angle = 180;
break;
case 65: // A key
newX = currRectX - moveB;
newY = currRectY;
angle = 180;
break;
case 40: // downkey
newX = currRectX;
newY = currRectY + moveA;
angle = 270;
break;
case 83: // S key
newX = currRectX;
newY = currRectY + moveB;
angle = 270;
break;
case 39: //rightkey
newX = currRectX + moveA;
newY = currRectY;
break;
case 68: // D key
newX = currRectX + moveB;
newY = currRectY;
break;
default:
return;
}
现在我正在试图找出如何将其添加到html按钮,例如,左键执行键和左键的操作。
我已经尝试了jQuery
这样的
$('#left-btn').click();
$('#right-btn').click();
但我不知道如何传递我已经编码为click()
的参数。
答案 0 :(得分:0)
你可以做你想做的一切。
不要忘记将变量声明为该函数的范围。
$('#left-btn').click(function(){
//do your things here
//e.g.
x -= 10;
angle = 50;
});