gpt4 book ai didi

html按钮中的javascript箭头键移动

转载 作者:行者123 更新时间:2023-11-28 03:24:53 25 4
gpt4 key购买 nike

我是 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()

最佳答案

你可以做任何你想做的事。
不要忘记在该函数范围之外声明变量。

$('#left-btn').click(function(){
//do your things here
//e.g.
x -= 10;
angle = 50;
});

查看更多:
https://api.jquery.com/click/

关于html按钮中的javascript箭头键移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45022835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com