Jump to content

Touch navigation in Phaser


universaljs
 Share

Recommended Posts

I needs to do simulations on the key keyboard. I know how to do it in a clean js:

/* Touch Screen */var bodi=document.getElementsByTagName("body")[0];var triggerKeyboardEvent = function(el,keyC,typeKey){	var eventObj=document.createEvent("Events");	eventObj.initEvent(typeKey, true, true);	eventObj.keyCode = keyC;	eventObj.which = keyC;	el.dispatchEvent(eventObj);   };   var buttonLeft=document.getElementById("button-left"),	buttonRight=document.getElementById("button-right"),	buttonUp=document.getElementById("button-up");buttonLeft.addEventListener('touchstart',function(e){triggerKeyboardEvent(bodi,37,"keydown");e.preventDefault();},false);buttonLeft.addEventListener('touchend',function(e){triggerKeyboardEvent(bodi,37,"keyup");e.preventDefault();},false);buttonRight.addEventListener('touchstart',function(e){triggerKeyboardEvent(bodi,39,"keydown");e.preventDefault();},false);buttonRight.addEventListener('touchend',function(e){triggerKeyboardEvent(bodi,39,"keyup");e.preventDefault();},false);buttonUp.addEventListener('touchstart',function(e){triggerKeyboardEvent(bodi,38,"keydown");e.preventDefault();},false);  buttonUp.addEventListener('touchend',function(e){triggerKeyboardEvent(bodi,38,"keyup");e.preventDefault();},false);/* End Touch Screen */

But I know that Phaser offers its own methods. 

    buttonRight = game.add.button(100, 300, 'buttonRight', function(){}, this);    buttonRight.fixedToCamera = true;    buttonRight.input.onTouchStart=function(){triggerKeyboardEvent(bodi,39,"keydown");}    buttonRight.input.onTouchEnd=function(){triggerKeyboardEvent(bodi,39,"keyup");} 

This of course does not work ...

Can anyone help me?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...