Help with Line path

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]

Have you tried using .isVisible?

On all the sections of the path that the ball moves over you could set .isVisible to false. [import]uid: 68741 topic_id: 14296 reply_id: 52868[/import]

I just tried that and it didn’t work! I don’t think I have it done right… Can you take a look notts_forest_
[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)
path.isVisible = true
end
end
end

Runtime:addEventListener(“touch”, drawLine)

–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
path.isVisible = false
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] [import]uid: 51459 topic_id: 14296 reply_id: 52882[/import]

I think you need path[poscount].isVisible = false

before you do poscount = poscount + 1
[import]uid: 19626 topic_id: 14296 reply_id: 52887[/import]

Hey Rob! Now the ball doesn’t move when I add path[poscount].isVisible = false before poscount = poscount + 1… :frowning: [import]uid: 51459 topic_id: 14296 reply_id: 52896[/import]

Actually that’s not going to work and it appears to me that the line’s can’t be erased.

If you look at where the line drawing happens, its a local variable called line that is scoped inside a for loop, inside the function drawLine.

So as you move your finger around the display (or the mouse in the sim) hundreds of line objects are being created, drawn and then lost. This is a prime example of a memory leak. Each of those line objects exist, but you no longer have a reference to them to remove them.

The path table is just a set of x, y coordinates. So hiding anything from “path” isn’t going to display anything since path has no display objects.

The function drawLine is also doing an i=i+1 which in itself is okay, but right below it, there is a for i,v in ipairs() loop that is overwriting i. It looks like this code is trying to use “i” for two different purposes. I’m trying to figure this code out. It appears there was plans to store the line in the endPoints array since on the table insert, they are setting line to nil.
[import]uid: 19626 topic_id: 14296 reply_id: 52902[/import]

Hey Rob, memory leeks are no good!! :frowning:

What if I have something like this?

[lua]local ui = require(“ui”)

local maxPoints = 2
local lineThickness = 1
local endPoints = {}
local path = {}
local i =1;local j =1
local traced = 1

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

if 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
print(path[i].x)
i=i+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

if(event.phase == “ended”) then
while(#endPoints > 0) do
table.remove(endPoints)

end

end
end
end
end

Runtime:addEventListener(“touch”, drawLine)
–Runtime:addEventListener(“touch”, traceClicks)

local animate
local posCount = 1
–moving the object – this is where animation takes place
local function moveObject(event)
if posCount <= #path then
c.x = path[posCount].x
c.y = path[posCount].y
posCount = posCount + 1
else
timer.cancel(animate)
end
end

– to move ball when we click the animate text
local function move(event)

if(event.phase == “ended”) then
posCount = 1

path[1] = {}
path[1].x = event.x
path[1].y = event.y
transition.to(c, { time = 500, x= path[1].x, y =path[1].y, onComplete= anim})

end
return true
end
–end

text:addEventListener(“touch”, move)[lua] [import]uid: 51459 topic_id: 14296 reply_id: 52910[/import]

leak* [import]uid: 51459 topic_id: 14296 reply_id: 52911[/import]

this code doesn’t show the lines does that mean no leaks?
[lua]local ui = require(“ui”)

local maxPoints = 2
local lineThickness = 1
local endPoints = {}
local path = {}
local i =1;local j =1
local traced = 1

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

if 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
print(path[i].x)
i=i+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

if(event.phase == “ended”) then
while(#endPoints > 0) do
table.remove(endPoints)

end

end
end
end
end

Runtime:addEventListener(“touch”, drawLine)

local animate
local posCount = 1
–moving the object – this is where animation takes place
local function moveObject(event)
if posCount <= #path then
ball.x = path[posCount].x
ball.y = path[posCount].y
posCount = posCount + 1
else
timer.cancel(animate)
end
end

– to move ball when we click the animate text
local function move(event)

if(event.phase == “ended”) then
posCount = 1

path[1] = {}
path[1].x = event.x
path[1].y = event.y
transition.to(ball, { time = 500, x= path[1].x, y =path[1].y, onComplete= anim})

end
return true
end
–end
local text = display.newText( “Goal!”, 220, 50, nil, 40 )
text.x = 450; text.y = 425
text:addEventListener(“touch”, move)[/lua] [import]uid: 51459 topic_id: 14296 reply_id: 52918[/import]

I got it working. I’ll post the code later

[import]uid: 19626 topic_id: 14296 reply_id: 52919[/import]

Thanks Rob!! [import]uid: 51459 topic_id: 14296 reply_id: 52934[/import]

Here ya go!

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  
 idx = 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  
  
 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  
 path[traced].line = line  
 end  
 traced = traced + 1  
 elseif(event.phase == "ended") then   
 while(#endPoints \> 0) do  
 table.remove(endPoints)  
 path.isVisible = true  
 end  
 end  
end  
   
Runtime:addEventListener("touch", drawLine)  
   
   
--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  
 if path[posCount].line then  
 path[posCount].line.isVisible = false  
 path[posCount].line:removeSelf()  
 path[posCount].line = nil  
 end  
 posCount = posCount + 1  
 --path.isVisible = false  
 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!", 100, 50, nil, 40 )  
text.x = 100; text.y = 425  
text:addEventListener("touch", move)  

Okay basically in the line objects now get recorded as part of the “path” array so we can remove them later. I had to move the traced = traced + 1 line to later in the function.

Tben when you move the ball, it hides and frees the lines so it shouldn’t leak memory now.

Rob
[import]uid: 19626 topic_id: 14296 reply_id: 52938[/import]

Thank you so much much for helping me out you are so AWESOME! I owe you big time Rob!! [import]uid: 51459 topic_id: 14296 reply_id: 52972[/import]

Hey Rob! Just ran a test this morning and it gave me this error
Runtime error
…ZS-a189GNQo1k+++TI/-Tmp-/TemporaryItems/324/main.lua:64: attempt to call global ‘goalReached’ (a nil value)
stack traceback:
[C]: in function ‘goalReached’
…ZS-a189GNQo1k+++TI/-Tmp-/TemporaryItems/324/main.lua:64: in function ‘_listener’
?: in function <?:446>
?: in function <?:215>

Should I be worried about this?

p.s Hope you are somewhere safe for the hurricane… :slight_smile: [import]uid: 51459 topic_id: 14296 reply_id: 53096[/import]

Yes Jake you need to worry about it.

Your program is trying to call a function “goalReached” which doesn’t exist. I assumed that you either didn’t include it or haven’t written it yet. But you need to do something when the goal is reached.

What do you want it to do? [import]uid: 19626 topic_id: 14296 reply_id: 53116[/import]

I will be removing the rocket and adding points of course… But thats when I add it to the game. I thought it had to do with that. Cool well thanks for all your help Rob!! Your AWESOME!!

I GOT A GREAT SKELETON TO WORK FROM NOW!! I OWE YOU BIG TIME!! [import]uid: 51459 topic_id: 14296 reply_id: 53236[/import]

Wow guys this is great code! This actually helps me out a ton with something I’ve been struggling with myself - so thanks for all your hard work on this!

The Corona community is awesome! [import]uid: 97023 topic_id: 14296 reply_id: 76042[/import]

no problem @ taddmencer!! [import]uid: 51459 topic_id: 14296 reply_id: 76044[/import]

Well nuts … now I have a questions.

In this example when yo uclick goal ,even if you’ve made two separate paths it will snap to the beginning …

If I moved my object, hit the goal … it moves.
I create a new path from the current location, hit goal - I’d like it to move from that second path, not the first.

Am I speaking gibberish? Is this pretty much redoing this entire code?? [import]uid: 97023 topic_id: 14296 reply_id: 76048[/import]

path’s are very hard to get around…This is the reason why i ended up scrapping the project… if you want i can send you what i got… i created a smooth path… i also got into multiple paths for multiple goals… but it got way too confusing for me and the person i was working with… we had to scrap it because we needed to make everything that went into the path code unique and the code got way to long and messy, AND CONFUSING!!!..
[import]uid: 51459 topic_id: 14296 reply_id: 76060[/import]