Can someone explain dragging to me.....

For having done this a while I should be able to figure this out… But my density filter has kicked in and something this simple is kicking my butt.
Most drag examples I’ve seen works basically like this

on Began phase, save the starting X, Y of the object back into itself as .x0 and .y0.

During the move phase, set the target.x and y to event.x-target.x0, event.y to event.y-target.y0.

Why don’t we just set the target.x, y to the event.x, y? What is this extra math step doing?

It appears that if my mouse cursor stays close to the target it moves correctly, but the further away from my player (draggable) object there appears to be a lag where the object falls behind the touch event. You run out of real estate and let up only to have to click in the object again.

Thoughts? [import]uid: 19626 topic_id: 21741 reply_id: 321741[/import]

Hey Rob,

The difference between coding like this;
[lua]display.setStatusBar(display.HiddenStatusBar)

local obj = display.newCircle( 160, 20, 20 )

local function drag (event)
if event.phase == “began” then
t = event.target
t.x0, t.y0 = event.x-t.x, event.y-t.y
elseif event.phase == “moved” then
t.x=event.x-t.x0
t.y=event.y-t.y0
end
end
obj:addEventListener(“touch”, drag)[/lua]

and like this;

[lua]display.setStatusBar(display.HiddenStatusBar)

local obj = display.newCircle( 160, 20, 20 )

local function drag (event)
event.target.x, event.target.y = event.x, event.y
end
obj:addEventListener(“touch”, drag)[/lua]

Is that with that first set of code you can grab the object anywhere, say the bottom left corner of a rectangle - and that is where you will continue “holding” onto it.

With the second set of code you’re simply stating the x and y of the object is the x and y of the event, so it would be the object’s center at the touch coordinates.

Using a larger circle and grabbing it towards the top, bottom, left or right you’d see a big difference.

I hope this is useful and not too long winded for you :wink:

Peach :slight_smile: [import]uid: 52491 topic_id: 21741 reply_id: 86337[/import]

I think I see. The net effect is that if you touch the object at its perimeter, the object will appear to jump to where you touched it as it re-aligns it’s center.

And if you’re using a camera, it can cause the whole world to jump too which is a bad effect.

Now to figure out why the object doesn’t keep up with the mouse click and drag (which seems to cause my teleportation bug…)

[import]uid: 19626 topic_id: 21741 reply_id: 86340[/import]

@Rob with your current code, set up another 1 or 2 objects and set the same touch handler to them (after all your code is generic, it uses target right)?

Now move the object and on the way touch the other objects, it will collect them too…

For a proper drag, you have to consider a lot more things. [import]uid: 3826 topic_id: 21741 reply_id: 86396[/import]