I have this code working exactly the way I want expect 1 minor detail… I want the path to delete when ball passes through it… Can someone please help…
[lua]local maxPoints = 2
local lineThickness = 5
local endPoints = {}
local path = {}
local i =1
local traced = 1
local animate
local posCount = 1
local messageTween
–create a circle ball
local ball = display.newCircle( 150, 150, 15 )
local function drawLine(event)
if event.phase == “began” then
for i=1, traced, 1 do
table.remove (path ,i)
end
i = 1
traced = 1
elseif event.phase == “moved” then
table.insert(endPoints, 1, {x = event.x, y = event.y, line= nil})
path[i] = {}
path[i].x = event.x
path[i].y = event.y
i=i+1
traced = traced + 1
if(#endPoints > maxPoints) then
table.remove(endPoints)
end
for i,v in ipairs(endPoints) do
local line = display.newLine(v.x, v.y, event.x, event.y)
line:setColor( 255, 255, 255, 255 )
line.width = lineThickness
end
elseif(event.phase == “ended”) then
while(#endPoints > 0) do
table.remove(endPoints)
end
end
end
Runtime:addEventListener(“touch”, drawLine)
local goalReached = function( xLoc, yLoc )
– This function is called when a flying object has successfully arrived at
– the correct destination.
– The Code Below Shows Floating Score “+1000” (etc) display
local xLoc = xLoc
local yLoc = yLoc
local writeTextAbovePlanet = function( textLineOne, theX, theY )
local theString = textLineOne
local theX = theX
local theY = theY
local screenMessage = display.newText( theString, 160, 240, “080203”, 44 )
screenMessage:setTextColor( 255, 255, 255, 255 )
screenMessage.alpha = 1.0
screenMessage.xScale = 0.5; screenMessage.yScale = 0.5
screenMessage.xOrigin = theX
screenMessage.yOrigin = theY
local destroyMessage = function()
if screenMessage ~= nil then
screenMessage:removeSelf()
screenMessage = nil
end
end
local newY = theY - 35
messageTween = transition.to( screenMessage, { time=1000, alpha=0, y=newY, onComplete=destroyMessage } )
end
writeTextAbovePlanet( “+1000”, xLoc, yLoc )
end
–moving the object – this is where animation takes place
local function moveObject(event)
if posCount < traced then
ball.x = path[posCount].x
ball.y = path[posCount].y
posCount = posCount + 1
else
if animate then
timer.cancel(animate)
goalReached()
end
end
end
function anim()
animate = timer.performWithDelay( 10, moveObject, traced )
end
– to move ball when we click the animate text
local function move(event)
if(event.phase == “ended”) then
posCount = 1
transition.to(ball, { time = 500, x= path[1].x, y =path[1].y, onComplete= anim})
end
return true
end
–create a text which when clicked will animate the object
local text = display.newText( “Goal!”, 220, 50, nil, 40 )
text.x = 450; text.y = 425
text:addEventListener(“touch”, move)[/lua]
Thanks [import]uid: 51459 topic_id: 14296 reply_id: 314296[/import]
[import]uid: 51459 topic_id: 14296 reply_id: 52896[/import]
[import]uid: 51459 topic_id: 14296 reply_id: 53096[/import]