Jump to content

getting touch position


elgraniaco
 Share

Recommended Posts

Hi,

I was wondering if there's a built-in way to get the touch position the same way stage.getMousePosition() does with the mouse.

I am using stage.getMousePosition() in my update function to get the mouse position constantly and I would need to mirror the behavior in mobile.

 

Many thanks.

Link to comment
Share on other sites

  • 2 weeks later...

I ended up using this strategy:

var currentMousePos = { x: -1, y: -1 };$(function () {  document.addEventListener("touchstart", onTouchStart, true);  document.addEventListener("touchend", onTouchEnd, true);  document.addEventListener("touchmove", onTouchMove, true);}function onTouchStart(event){  currentMousePos.x = event.pageX;  currentMousePos.y = event.pageY;}function onTouchMove(event){  currentMousePos.x = event.pageX;  currentMousePos.y = event.pageY;}function onTouchEnd(event){  currentMousePos.x = event.pageX;  currentMousePos.y = event.pageY;}

 

Link to comment
Share on other sites

Could you use `sprite.data.getLocalPosition(sprite.parent)` ?

 var pointer = sprite.data.getLocalPosition(sprite.parent);

You then end up with `pointer.x` and `pointer.y` that tell you the position of either the mouse event, touch event, or both.

 

It should work for any DisplayObject, not just sprites.

Link to comment
Share on other sites

First of all, thank you very much for the replies.

The approach to a solution I used is something in the lines of samim's, my question was whether PIXI had a built-in function like stage.getMousePosition() in order to simplify my code (I would only have to add one line of new code to my "on mouse hold" position catching logic). I've done my research though and found out there isn't.

 

Again, thank you very much.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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