Hi, in a level of my game, players have to draw a line for connects 2 dots. I think I will need to use a collision event, but…how?
here’s the code for the line, and the dot will be called dot.png
local lines = {}
local lineGroup = display.newGroup()
local line\_number = 1
local line\_width = 5
local prev\_x, prev\_y, ball
local isDrawing = false
local i = 1
local function draw\_line(e)
if e.phase == "began" then
for i = #lines, 1, -1 do
if (lines[i]) then
lines[i].parent:remove(lines[i])
lines[i] = nil
end
end
lines = {}
line\_number = 1
prev\_x = e.x
prev\_y = e.y
isDrawing = true
elseif e.phase == "moved" then
lines[line\_number] = display.newLine(prev\_x, prev\_y, e.x, e.y)
lines[line\_number]:setColor(255, 255, 255)
lines[line\_number].width = line\_width
dist\_x = e.x - prev\_x
dist\_y = e.y - prev\_y
-- Add a physics body that's a flat polygon that follows each segment of the line
physics.addBody(lines[line\_number], "static", { density = 1, friction = 0.5, bounce = 0, shape = {0, 0, dist\_x, dist\_y, 0, 0} } )
prev\_x = e.x
prev\_y = e.y
line\_number = line\_number + 1
elseif e.phase == "ended" then
end
end
Runtime:addEventListener("touch", draw\_line)
Then, when the dot is reached by the line, I will print on the screen an “OK” message.
Thanks
[import]uid: 76800 topic_id: 31739 reply_id: 331739[/import]