如何将键盘控件转换为触摸

时间:2018-12-21 15:10:13

标签: javascript html5 mobile touch

我正在尝试将一个简单的游戏转换为可响应触摸的游戏。

该游戏从左向右射击,它是一个太空侵略者克隆。

 function KeyBoard() {
        var state = {};
         window.addEventListener('keydown', function(e) {
             state[e.keyCode] = true;
         });
         window.addEventListener('keyup', function(e) {
             state[e.keyCode] = false;
         });
         this.isDown = function (key) {
            return state[key];
        };
    }
    KeyBoard.KEYS = {
        LEFT: 37,
        RIGHT: 39,
        SPACE: 32
    };

    window.addEventListener('load', function() {
        new Game();
    });
}

这是基本控制。

我也有以下代码,用于通过触摸同步动作

var touchedElement = document.getElementById("myElementToBeTouched");
  touchedElement.addEventListener("touchstart", funcTouchStart, false);
  touchedElement.addEventListener("touchend", funcTouchEnd, false);
  touchedElement.addEventListener("touchmove", funcTouchMove, false);

  function funcTouchStart(e) {
    //code to do what you want like set variables and check where on screen             
    touch happened

     var touches = e.changedTouches; //gets array of touch points, to get           
position
  }

  function funcTouchEnd(e) {
    //code
  }

  function funcTouchMove(e) {
    //code
  }

但是,我完全无法使它正常工作: https://codepen.io/MrVincentRyan/pen/VqpMrJ

0 个答案:

没有答案