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]
[import]uid: 3826 topic_id: 9965 reply_id: 36400[/import]