Cannot position an item in a group using contentToLocal

I am going crazy!!! I have group “P” which I want to zoom to fill the screen. It is sitting in another group. I have tried to set the x,y of the group P to the center of the screen, by using contentToLocal, but it simply does not work.

So I tried a little test, below.

local screenW, screenH = display.contentWidth, display.contentHeight  
  
local g = display.newGroup()  
  
local r = display.newRect(0,0,100,100)  
r:setFillColor(100,200,100,150)  
g:insert(r)  
  
r:setReferencePoint(display.CenterReferencePoint)  
r.x = 0  
r.y = 0  
g:setReferencePoint(display.CenterReferencePoint)  
g.x = 200  
g.y = 50  
local x,y = g:contentToLocal(screenW/2,screenH/2)  
transition.to(g, {x=x, y=y} )  
print (x,y)  

This code prints the result, “-40, 190”, which is the correct x,y.

However, moving the rectangle, r, instead of the group, actually works fine:

local x,y = r:contentToLocal(screenW/2,screenH/2)  
transition.to(r, {x=x, y=y} )  

What is going on here?

So, as I wrote this, I figured out that adding the results to g.x will work. That is, -40 + g.x is the center of the screen. Assuming I’m dealing with a group, which my code shouldn’t have to check for. I’m worried there is a bug here. Please tell me what is going on.
[import]uid: 37366 topic_id: 16879 reply_id: 316879[/import]

I think it’s doing what it’s supposed to be doing, although that’s not what you want it to be doing, it seems. It sounds like your test case isn’t actually replicating what you want to accomplish, but hopefully this will help:

contentToLocal takes the given coordinates and converts them into coordinates local to the object calling the function. So you’re basically saying in that code, “where is the center of the screen relative to g?” Or put another way, “how much do I need to move g along the x and y axes to reach the centre of the stage?” And the answer is that you need to move g -40 pixels from its current position along the x axis and 190 pixels from its current position along the y axis. So they’re relative coordinates, not absolute coordinates, but when you pass them to your transition, you’re treating them as absolute coordinates, which is why g ends up where it does.

It sounds like what you want to do is move r to the (visual) centre of the screen without changing the position of g, its display group. This is why the second piece of code, where you’re using r instead of g, works. In the test code you provide, the display group g isn’t inside any other containers - it’s right on the stage. So, you don’t even need to use the contentToLocal() call to figure out how much it needs to move.

Here’s the fun part: if you want to move r to the (visual) centre of the stage, without moving g, then you’re halfway there: you need to use g when calling contentToLocal, but then you take the result and apply it to r:

[lua]local x,y = g:contentToLocal(screenW/2,screenH/2)
transition.to(r, {x=x, y=y} )[/lua]

(In your second bit of code, the only reason why you can get away with using r in the contentToLocal() call is because r is currently at 0, 0. Change r.x to 100 and try it again to see how it goes off the rails. But! If you use the code I pasted in above, then regardless of where you put r, it will move to the centre of the stage, because it’s moving within the coordinate space of g, and in g, to move something to look like it’s in the centre of the screen, you need to move it to -40, 190.

( Note : there is some funkiness going on because you’re setting the reference point on g. For some reason, it’s resetting the position of r back to 0, 0. This appears to be a bug, but you’d need to do further testing to confirm that. Since the default reference point is the centre of a display object anyway, both of those calls are unnecessary, so bug or not, you can safely remove them.)

Thanks,
Darren [import]uid: 1294 topic_id: 16879 reply_id: 63301[/import]