I have 100 nodes that are randomly spawned across the screen and I am trying to draw a line between each of the neighboring nodes. I am able to connect the neighbors using display.newLine but I am having trouble updating each line as the nodes move. This picture shows my output when the lines are drawn. Next I remove the lines and redraw them as the nodes move.
The code I have below works but it causes massive amounts of lag every time the For loops get called.
[lua]
local function connectNeighbors()
local isNeighbor = false
– compare each nodes position to all other nodes
for index = 1, 100 do
for index2 = 1, 100 do
isNeighbor = hasCollidedCircle( nodeTable[index], nodeTable[index2] ) – returns true if objects are in same radius
if ( isNeighbor == true ) then
– draw line between nodes
local nodeLink = display.newLine( mainGroup, (nodeTable[index]).x, (nodeTable[index]).y, (nodeTable[index2]).x,
(nodeTable[index2]).y )
– set line color to blue
nodeLink:setStrokeColor( 0, 0, .8 )
function nodeLink:timer()
nodeLink:removeSelf()
end
– remove line after so long
linkTimer = timer.performWithDelay( 1000, nodeLink, 1)
isNeighbor = false
end
end
end
end[/lua]
Is there a better way to do this that won’t cause so much lag? I have also tried using physics to give each node a large radius and draw a line when a collision is detected, but I can’t get the collision to re-trigger while the nodes remain next to each other.
Any feedback would be great. Thank you.