Question about touch and drag

Hi-I am very new to Corona and have a pretty basic question:

I wanted to play with touch functions and have an object follow my finger(or mouse). I found under the api area of the corona site:

Syntax:
event.x
Example:
The following code creates a green circle that follows the user’s finger as it moves across the screen.

local circle = display.newCircle(50, 50, 100)
circle:setFillColor(0,255,00)
function moveCircle( event )
circle.x = event.x
circle.y = event.y
end
Runtime:addEventListener(“touch”, moveCircle)

Anyways when you first click the circle it jumps to the mouse. So after more research I found another tutorial and tried this another way that does work but I am not sure I understand everything. I was hoping for a small explanation on the “store initial position”
part(line 8). What is this doing and what is .x0? I tried searching the corona site for an explanation of this but came up empty. I know this probably has a simple explanation and I am just not seeing it. Thanks ahead of time!

1.local function drag( event )
2. local ball = event.target
3.
4. local phase = event.phase
5. if “began” == phase then
6. display.getCurrentStage():setFocus( ball )
7.
8. – Store initial position
9. ball.x0 = event.x - ball.x
10. ball.y0 = event.y - ball.y
11.
12. – Avoid gravitational forces
13. event.target.bodyType = “kinematic”
14.
15. – Stop current motion, if any
16. event.target:setLinearVelocity( 0, 0 )
17. event.target.angularVelocity = 0
18.
19. else
20. if “moved” == phase then
21. ball.x = event.x - ball.x0
22. ball.y = event.y - ball.y0
23. elseif “ended” == phase or “cancelled” == phase then
24. display.getCurrentStage():setFocus( nil )
25. event.target.bodyType = “dynamic”
26. end
27. end
28.
29. return true
30.end
31.ball:addEventListener(“touch”, drag)
[import]uid: 39370 topic_id: 9965 reply_id: 309965[/import]

I haven’t tested this code, but it seems like x0, y0 is to avoid the object
sudden movement when the user touches the screen. [import]uid: 12455 topic_id: 9965 reply_id: 36360[/import]

@mattgames,
the .x0 and .y0 are custom variables added to the object to store the initial position of the touch.

In other languages you might have to create variables to store, here that is stored on the object, so if you had several objects using the same code, each will have a unique .x0 and .y0. There are some definite advantages of using Lua over other languages, specially dynamic variable creation.

?:slight_smile: [import]uid: 3826 topic_id: 9965 reply_id: 36400[/import]

Thank you both for the quick responses-I understand it better now. I should probably post a new question but I will try here first. Now that I have the drag function working I noticed that if I use an image and click just outside the image in the alpha area of the png I am able to move it. Any idea how to fix this?

Thanks again. [import]uid: 39370 topic_id: 9965 reply_id: 36591[/import]

I think I found the answer after searching more forum posts. I need to use polygons in a group positioned around the image. [import]uid: 39370 topic_id: 9965 reply_id: 36593[/import]