I have some code for a drag event and I can understand what the code is doing but i am slightly confused on the theory behind the ‘the initial location’ and the if “moved” part.
This line of code is the part that I need explaining, I have noticed a few people use it but I cant find the theory behind it:
‘t.x0 = event.x - t.x and the t.x = event.x - t.x0’.
I would be very grateful if someone could help me out with an explanation so that I have a better understanding of what I am writing.
Here is the full code for that section:
[lua]–basic dragging physics
local function startDrag (event)
local t = event.target
local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus(t)
t.isFocus = true
–store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
–make the body type ‘kinematic’ to avoid gravity problems
event.target.bodyType = “kinematic”
–stop current motion
event.target:detLinearVelocity (0,0)
event.target.angularVelocity = 0
else if t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0
else if “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus (nil)
t.isFocus = false
–switch body type back to “dynamic”
if (not event.target.isPlatform) then
event.target.bodyType = “dynamic”
end
end
end
return true
end [/lua]