将屏幕键盘按钮添加到网站

时间:2019-07-25 07:21:16

标签: game-physics pixijs

我正在尝试添加屏幕上的按钮,以在我的网站上移动精灵。 我尝试使用jQuery添加用于播放键代码的按钮,但到目前为止,它们都没有起作用。 https://alfielytorres.github.io/hackercastle/

现在我的代码如下

//load keyboard
class Keyboard {
    constructor() {
        this.pressed = {};
    }
    watch(el) {
        el.addEventListener('keydown', (e) => {
            this.pressed[e.key] = true;
        });
        el.addEventListener('keyup', (e) => {
            this.pressed[e.key] = false;
        });

    }



app.loader.load((loader, resources) => {
    // create a new keyboard instance
    let kb = new Keyboard();
    kb.watch(app.view);

.....

});



...

    //Jumping
        if (characterPosition.vy < 0) {
            characterPosition.y += characterPosition.vy;
        }
        if (!kb.pressed.ArrowUp && touchingGround && characterPosition.jumped) {
            characterPosition.jumped = false;
        }
        if (kb.pressed.ArrowUp && touchingGround && !characterPosition.jumped) {

            characterPosition.vy = -19;
            characterPosition.jumped = true;
        }

        //Right
        if (kb.pressed.ArrowRight) {
            characterPosition.direction = 0;
            characterPosition.vx = Math.min(8, characterPosition.vx + 2);
        }
        if (characterPosition.vx > 0) {
            characterPosition.vx -= 1;
        }
        //Left
        if (kb.pressed.ArrowLeft) {
            characterPosition.direction = 1;
            characterPosition.vx = Math.max(-8, characterPosition.vx - 2);
        }
        if (characterPosition.vx < 0) {
            characterPosition.vx += 1;
        }

0 个答案:

没有答案