How to find X/Y in scaled display group?

Hello,

First I added a scrollView that is 8000 each way in size. I set the position to 4000 for the middle. I then use the scaleTo for .9 to make it slightly smaller. I drag a tile in the group and let go. If I do not scale the display I can get the correct position just by offsetting the x/y by the amount of scroll. When I scale it to .9 I cannot determine how to do this. I’ve tried taking the final position and adding that value times .1 to it and moving it that much but thats not correct.

Can anyone tell me the calculation to make up the offset of a scaled object? I’m just not thinking straight for this.

Thanks,

Warren

Hi Warren,

Are you scaling the objects inside the scrollView, or scaling the entire scrollView? It’s not recommended that you scale the entire thing.

But as to answer your question, you should be able to just multiply the x/y in a scaled group by whatever you’ve scaled it by. For example, to get x/y of 100,100 in a group that you’ve scaled by 2x, just multiply each by 2 for a result of 200,200. Similarly, if you scaled the group by 0.78, that would equal 78,78 as the adjusted x/y.

Take care,

Brent

I put another display group inside of the scrollview and I am scaling the display group. I tried what you said and it doesn’t seem to be working. I scale the group to .98 and then multiply the position by that and it still goes to the left and up when i release the tile.

The display group is named bdgroup and I am scaling it this way:

transition.scaleTo( bgroup, { xScale=.98, yScale=.98, time=100 } )

Then for moving the tile and dropping it I am using this code. When the tile is dropped I am getting the scrollview position to offset the actual position, insert the tile into the group and place it there. What I did works fine when nothing is scaled. Then when I scale and add the code to multiply by .98 it goes up and left when dropped.

local function onTileTouch( event ) local t = event.target local phase = event.phase if "began" == phase then -- Make target the top-most object local parent = t.parent parent:insert( t ) display.getCurrentStage():setFocus( t ) -- Spurious events can be sent to the target, e.g. the user presses -- elsewhere on the screen and then moves the finger over the target. -- To prevent this, we add this flag. Only when it's true will "move" -- events be sent to the target. t.isFocus = true -- Store initial position t.x0 = event.x - t.x t.y0 = event.y - t.y elseif t.isFocus then if "moved" == phase then -- Make object move (we subtract t.x0,t.y0 so that moves are -- relative to initial grab point, rather than object "snapping"). t.x = event.x - t.x0 t.y = event.y - t.y0 elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false bgroup:insert( t ) --Tried this here and also below 5 lines. Same results. t.x = t.x \* .98 t.y = t.y \* .98 local x, y = scrollView:getContentPosition() t.x = t.x + (x \* -1) - scrollView.x t.y = t.y + (y \* -1) - scrollView.y t:removeEventListener( "touch", onTileTouch ) end end -- Important to return true. This tells the system that the event -- should not be propagated to listeners of any objects underneath. return true end

Hi Warren,

Remember that, by default, display groups don’t have an anchor point (because they are considered unlimited in size, depending on what you put into them). You can set the “.anchorChildren” property to true to change this, then set a central anchor point on the group (0.5 for both .anchorX and .anchorY). See if that does more of what you wish… and remember, setting the “.anchorChildren” property will usually offset the group’s position, so you’ll need to re-position it.

Brent

Hi Warren,

Are you scaling the objects inside the scrollView, or scaling the entire scrollView? It’s not recommended that you scale the entire thing.

But as to answer your question, you should be able to just multiply the x/y in a scaled group by whatever you’ve scaled it by. For example, to get x/y of 100,100 in a group that you’ve scaled by 2x, just multiply each by 2 for a result of 200,200. Similarly, if you scaled the group by 0.78, that would equal 78,78 as the adjusted x/y.

Take care,

Brent

I put another display group inside of the scrollview and I am scaling the display group. I tried what you said and it doesn’t seem to be working. I scale the group to .98 and then multiply the position by that and it still goes to the left and up when i release the tile.

The display group is named bdgroup and I am scaling it this way:

transition.scaleTo( bgroup, { xScale=.98, yScale=.98, time=100 } )

Then for moving the tile and dropping it I am using this code. When the tile is dropped I am getting the scrollview position to offset the actual position, insert the tile into the group and place it there. What I did works fine when nothing is scaled. Then when I scale and add the code to multiply by .98 it goes up and left when dropped.

local function onTileTouch( event ) local t = event.target local phase = event.phase if "began" == phase then -- Make target the top-most object local parent = t.parent parent:insert( t ) display.getCurrentStage():setFocus( t ) -- Spurious events can be sent to the target, e.g. the user presses -- elsewhere on the screen and then moves the finger over the target. -- To prevent this, we add this flag. Only when it's true will "move" -- events be sent to the target. t.isFocus = true -- Store initial position t.x0 = event.x - t.x t.y0 = event.y - t.y elseif t.isFocus then if "moved" == phase then -- Make object move (we subtract t.x0,t.y0 so that moves are -- relative to initial grab point, rather than object "snapping"). t.x = event.x - t.x0 t.y = event.y - t.y0 elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false bgroup:insert( t ) --Tried this here and also below 5 lines. Same results. t.x = t.x \* .98 t.y = t.y \* .98 local x, y = scrollView:getContentPosition() t.x = t.x + (x \* -1) - scrollView.x t.y = t.y + (y \* -1) - scrollView.y t:removeEventListener( "touch", onTileTouch ) end end -- Important to return true. This tells the system that the event -- should not be propagated to listeners of any objects underneath. return true end

Hi Warren,

Remember that, by default, display groups don’t have an anchor point (because they are considered unlimited in size, depending on what you put into them). You can set the “.anchorChildren” property to true to change this, then set a central anchor point on the group (0.5 for both .anchorX and .anchorY). See if that does more of what you wish… and remember, setting the “.anchorChildren” property will usually offset the group’s position, so you’ll need to re-position it.

Brent