Jump to content

How to detect holding down the input on a sprite on mobile?


erikwittern
 Share

Recommended Posts

Hi there,

 

I would like to react to the player holding down the input on a sprite (e.g., called obj, with inputEnabled = true). On a desktop, I can achieve this for the mouse using:

if(obj.input.pointerDown()){  // do something...}

On mobile, however, the code does not work - I believe I would have to use something like pointer1, but it seems only to be accessible for the game object, for example:

game.input.pointer1.isDown

Is there a way to activate/use pointer1 on sprites as well?

Link to comment
Share on other sites

You can have it set as this.input.pointer1.isdown

As long as you've assigned all the proper code for the input on the sprite ie: inputEnabled then I don't see why it shouldn't work. Maybe back up the code with a pointer1.isUp so it releases but that is pure speculation since. But in general it should work. I had a game made with draggable sprites and it wasn't too complex.

Link to comment
Share on other sites

I actually also tried a hack by defining pointer1 as the input of obj like so:

obj.input = game.input.pointer1;

Interestingly, this works fine. Until you want to change the state in the game - then, it tries to call input.destroy() on pointer1, which does not exist.

 

Any other ideas?

Link to comment
Share on other sites

I think I found my solution: I use the dragging functionality for my purpose (thanks, Heppell08, for hinting at this). So what I do is: I enable dragging with:

obj.input.enableDrag();

(Note: obj.input.draggable = true; does enable reacting to dragging as well, but produced errors in my case because some instantiations, e.g. of _dragPoint, are not performed - thus rather use enableDrag()).

Then, I can check for it (on desktop and mobile) using:

if(obj.input.isDragged){  // do something...}

Fortunately, obj.input.isDragged is true even if the pointer is just pressed, no matter of whether actual dragging happens, making this approach suitable for my purpose. As I do not actually want to drag something around (just detect the pointer being pressed), I also prohibit horizontal and vertical dragging using:

obj.input.setDragLock(false, false);
Edited by John135
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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