Get Touch Location in Nodes Location

I have a node called Move Area.
if i touch it i should calculate if the touch is left (behind), the sprite in the middle of the area, or right(in front) of it.
Just a simple move left or right touch control.( ← | ->)

The Problem is if i do this:

onTouchBegan(event){
        let delta1 = event.touch.getLocationX();
..................

it will give me back the Screen or OpenGL position.
for the calculation i need the position in node level.

my brain is smoking… :persevere:
can u give me any suggestions on how to solve it?

Or is there a way to get the Screen Coordinates of a Node?

I will ask engineering to take a look. What version of Creator are you using?

Use EventTouch’s getUILocation()

@slackmoehrle im using Creator Version 2.4.3
@StudioAMK Uncaught TypeError: event.touch.getUILocation is not a function…

I thought you were using the latest one.
Check API doc for your version.

https://docs.cocos.com/creator/api/en/classes/Event.EventTouch.html
as far as i see… there is nothing that can help me out here.

I checked your requirement again and I think, we can just

  • create two touch areas; one for “moving left” and another one for right.

—————-

If you must calculate, you can

  • transform world’s coordinate to local node’s coordinate (you must do more reading, there are many ways to do that. eg: by using convertToNodeSpace. Again, rotation, anchor point, scaling etc etc must put into consideration. )

I do hope it helps. :wink:

1 Like

WOW… OKAY…
First of all MUCH Thanks to you and especially to your words:

Again, rotation, anchor point, scaling etc etc must put into consideration.

I was reading a lot about this Topic in the Forum and in the Documentation.
But thats not very self complaining. The “etc etc” in your Answer have made me think

I had it allready like that… but my Problem was the camera zoom!
In my case it is on 0.6 static.

I forget that in the calculation before i compare the middle Sprite PositionX with the touch locationX…
now it works like a charm!

Here is the corrected part in my code:

let middle = cc.find("Canvas").convertToNodeSpaceAR(cc.find("Canvas/Main_Camera/Move_Area/Middle").getPosition());

let fingerpos = cc.find("Canvas").convertToNodeSpaceAR(event.touch.getLocation());
this.fingerpos_x = fingerpos.x / 0.6;

if(middle.x   < fingerpos_x){
//Move left
}
if(middle.x   > fingerpos_x){
//Move right
}

I thought about scaling and everything but not the cam zoom… :see_no_evil:
finally after 5 days of searching and fiddling! Tonight I’m sure I can sleep well.

1 Like

Glad to hear that you had solved it :hugs:

Share us your game once it’s done…

Cheers :clinking_glasses:

1 Like