localToContent vs contentToLocal - drives me nuts!

Hi fellows,

I am either stupid or just tired and old :wink:

I cant get this to work and have been fighting this for one day now. First of all I have a scrollable world thats around 3072 pixels wide and it is a display group. I am placing objects into this group by dragging and dropping - and somehow I managed to get that to work, but I guess the approach isn’t 100% bullet proof. Above this display group, I have placed a fixed “viewport”. I am using this viewport as a “container” - otherwise - I will lose my events and it will crash.

So second, and here comes my problem. I am picking up the object and start to drag it. What I want to do when I pick it up is to release it from the scrollable world (display group) - just so that my object can be on top of all the other groups in the viewport. There are groups as menus, drawers, etc. I do need to have that object above those, and it cant travel in the back. So thats why I am releasing it from the “main” group and adding it to the “viewport”. Enought about that now :wink:

And it’s now the problem starts. As soon as I pick it up and places the object in the viewport, it is misplaced. No matter what I try I cant get it to work.

  
if event.phase == "began" then  
  
 --Hokus pocus code....no matter what I try it wont be placed exactly  
  
 local x\_local, y\_local = event.target:localToContent(event.x, event.y )  
  
  
 -- Move the object into the viewport  
 viewPort:insert(event.target)  
 event.target.x = x\_local   
 event.target.y = y\_local  
 event.target:toFront();  
  
 --Remove it from the background group  
 mainGroup:remove(event.target)  
  
elseif....  
  

When I place it in the background I am using [lua]“contentToLocal”[/lua] - and I assume that I should use [lua]“localToContent”[/lua] now? But I am not sure where I should pick my x and y values from. The mainGroup, event.target, event ? Can someone please fill me in here and handle over the correct spell for this :slight_smile:

Regards, Joakim [import]uid: 81188 topic_id: 32579 reply_id: 332579[/import]

Try using 0,0 for your x,y values. 0,0 would be the center of the object you are picking up in it’s own local coordinates. So your line 5 would be

local x\_local, y\_local = event.target:localToContent(0,0)  

I think your variable names ’ x_local, y_local’ might be helping confuse you. You are really trying to get the ‘Content’ (screen) coordinates of the object you are pulling out, so i’d name them something like

local x\_content, y\_content = event.target:localToContent(0,0)  

As I understand how this works, that would convert the local coordinates 0,0 (center) of your object, which could be something like 2467,1895 or whatever in your giant scrolled background and gives you the screen (content) coordinates in return (something like 212,180 or whatever). So then you would be able to drag that object around on top of everything else. [import]uid: 9422 topic_id: 32579 reply_id: 129601[/import]

Try using 0,0 for your x,y values. 0,0 would be the center of the object you are picking up in it’s own local coordinates. So your line 5 would be

local x\_local, y\_local = event.target:localToContent(0,0)  

I think your variable names ’ x_local, y_local’ might be helping confuse you. You are really trying to get the ‘Content’ (screen) coordinates of the object you are pulling out, so i’d name them something like

local x\_content, y\_content = event.target:localToContent(0,0)  

As I understand how this works, that would convert the local coordinates 0,0 (center) of your object, which could be something like 2467,1895 or whatever in your giant scrolled background and gives you the screen (content) coordinates in return (something like 212,180 or whatever). So then you would be able to drag that object around on top of everything else. [import]uid: 9422 topic_id: 32579 reply_id: 129601[/import]