See if this helps:
main.lua:
[lua]
– move along path
require(“mathlib”)
require(“physics”)
physics.start()
physics.setGravity(0,0)
physics.setDrawMode(“hybrid”)
sWidth, sHeight = display.contentWidth, display.contentHeight
gravity = {x=0,y=10}
playerRadius = 10
joinThreshold = playerRadius
dots = display.newGroup()
lines = display.newGroup()
tracker = display.newGroup()
tracker.line = display.newGroup()
tracker.dot = display.newCircle(tracker,0,0,playerRadius)
physics.addBody(tracker.dot)
tracker.dot.linearDamping = 20
tracker.dot.lineIndex = 1
joint = physics.newJoint(“touch”,tracker.dot,tracker.dot.x,tracker.dot.y)
dot = tracker.dot
function findClosestLines()
local index, start, last = dot.lineIndex, dot.lineIndex, dot.lineIndex+1
if (index > 1 and lengthOf(dot,dots[index]) < joinThreshold) then
dot.lineIndex = dot.lineIndex - 1
return dot.lineIndex, last
end
if (index < dots.numChildren-1 and lengthOf( dot, dots[last] ) < joinThreshold) then
dot.lineIndex = dot.lineIndex + 1
return dot.lineIndex, dot.lineIndex + 1
end
return start, last
end
function findClosestPoint(e, start, last)
local start, last = findClosestLines()
local closestDist = 1000000
local closestPt = nil
if (start == last) then
return dots[start] – location of only dot
end
– find the line that the dot is on
for i=start, last-1 do
local pt = GetClosestPoint( dots[i], dots[i+1], e )
local len = lengthOf( pt, e )
if (len < closestDist) then
closestDist = len
closestPt = pt
end
end
return closestPt
end
function refreshTrack(e)
local pt = findClosestPoint(e)
joint:setTarget(pt.x,pt.y)
if (tracker.line) then tracker.line:removeSelf(); tracker.line = nil end
tracker.line = display.newLine(tracker,e.x,e.y,pt.x,pt.y)
end
function touch(e)
if (e.phase == “began”) then
refreshTrack(e)
elseif (e.phase == “moved”) then
refreshTrack(e)
else
if (tracker.line) then tracker.line:removeSelf() end
tracker.line = nil
end
return true
end
function tap(e)
local dot = display.newCircle(dots,e.x,e.y,15)
dot:setFillColor(0,0,0,0)
dot:setStrokeColor(0,255,0)
dot.strokeWidth = 5
while (lines.numChildren > 0) do
lines[1]:removeSelf()
end
if (dots.numChildren > 1) then
for i=2, dots.numChildren do
local line = display.newLine(lines,dots[i-1].x,dots[i-1].y,dots[i].x,dots[i].y)
line.width = 5
line:setColor(0,0,255)
end
end
if (dots.numChildren == 1) then
tracker.dot.x, tracker.dot.y = e.x,e.y
joint:setTarget(e.x,e.y)
elseif (dots.numChildren > 1) then
Runtime:addEventListener(“touch”,touch)
end
return true
end
Runtime:addEventListener(“tap”,tap)
[/lua] [import]uid: 8271 topic_id: 35544 reply_id: 145383[/import]