简体   繁体   English

切换到按钮控制游戏而不是键盘

[英]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.

相关问题 带有js的键盘开关按钮快捷方式 - keyboard switch button shortcut with js 由键盘控制的 Material UI 复选框 - Material UI Checkbox controlled by keyboard 有什么方法可以代替使用键盘键,而是使用屏幕上的按钮? - is there any way to instead of using a keyboard key, use a button on screen? 用图像替换JavaScript控制的按钮 - Replace javascript controlled button with image 如何使用Enter键而不是通过开关单击按钮 - How can I use Enter Key instead of clicking the button by switch 纸牌游戏的游戏循环,获胜者开始,1位用户控制的玩家 - Game loop for card game, winner starts, 1 user controlled player 使用切换开关(而不是按钮或链接)使bootsrap4.x导航药丸变为开关 - Make bootsrap4.x nav pills with toggle switch instead of button or link make it switch 组件正在更改要控制的电子邮件类型的不受控制的输入。 输入元素不应从不受控制切换为受控制 - A component is changing an uncontrolled input of type email to be controlled. Input elements should not switch from uncontrolled to controlled 一个组件正在将 SwitchBase 的不受控制的检查 state 更改为受控制。 元素不应从不受控制切换到受控 - A component is changing the uncontrolled checked state of SwitchBase to be controlled. Elements should not switch from uncontrolled to controlled 组件正在将文本类型的受控输入更改为不受控制。 输入元素不应从受控切换到不受控 - A component is changing a controlled input of type text to be uncontrolled. Input elements should not switch from controlled to uncontrolled
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM