elgraniaco

getting touch position

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.

Share this post


Link to post
Share on other sites

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;}

 

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.