problem with object:remove()

HI, I’ve used this code to let a curved line disappear when the user draw another one, and it works fine:

local lines = {}  
local line\_number = 1  
local line\_width = 10  
local prev\_x, prev\_y, ball  
   
local function draw\_line(e)  
   
 if e.phase == "began" then  
 prev\_x = e.x  
 prev\_y = e.y  
 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  
  
  
   
 elseif e.phase == "moved" then  
 lines[line\_number] = display.newLine(prev\_x, prev\_y, e.x, e.y)   
 --lines[line\_number]:setColor(math.random(255), math.random(255), math.random(255))  
 lines[line\_number]:setColor(255)  
 lines[line\_number].width = line\_width  
 dist\_x = e.x - prev\_x  
 dist\_y = e.y - prev\_y  
 physics.addBody(lines[line\_number], "static", { density = 1, friction = 0.5, bounce = -1.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  

now, I would like to do the same thing with a straight line, the problem is that when I start to draw a second line, the application crash, here’s the code I’ve used

local lines = {}  
local lineGroup = display.newGroup()  
local prevX,prevY  
local isDrawing = false  
local i = 0  
  
local function distanceBetween(x1, y1, x2, y2)  
 local dist\_x = x2 - x1  
 local dist\_y = y2 - y1  
 local distanceBetween = math.sqrt((dist\_x\*dist\_x) + (dist\_y\*dist\_y))  
 return distanceBetween  
end  
  
local function drawLine(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  
  
  
 prevX = e.x  
 prevY = e.y  
 isDrawing = true  
 i = i + 1  
 elseif(e.phase == "moved") then  
 local distance = distanceBetween(prevX, prevY, e.x, e.y)  
 if(isDrawing and distance \< 400) then  
 if(lines[i]) then lineGroup:remove(i) end  
 lines[i] = display.newLine(prevX, prevY, e.x, e.y)  
 lines[i]:setColor(255, 255, 255)  
 lines[i].width = 10  
  
 local dist\_x = e.x - prevX  
 local dist\_y = e.y - prevY  
 physics.addBody(lines[i], "static", { density = 1, friction = 0.5, bounce = -1.0, shape = {0, 0, dist\_x, dist\_y, 0, 0} } )  
 lineGroup:insert(lines[i])  
 end  
 elseif(e.phase == "ended") then  
 isDrawing = false  
 end  
end  
  
Runtime:addEventListener("touch",drawLine)  

Where’s my mistake? thanks! [import]uid: 76800 topic_id: 25344 reply_id: 325344[/import]