[Resolved] Problem with object:remove()

Hi, I’m using the following code to let players draw a line and to make it dissapear when the user starts drawing a new one, the problem is that when I’m drawing the second line, the first one dissapear, but the game crash,
how can I solve it? thanks.

[code]
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)
[/code] [import]uid: 76800 topic_id: 25682 reply_id: 325682[/import]

Try this;

[lua]local lines = {}
local lineGroup = display.newGroup()
local prevX,prevY
local isDrawing = false
local i = 1

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

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)[/lua]

Let me know if that helps.

Peach :slight_smile: [import]uid: 52491 topic_id: 25682 reply_id: 103849[/import]

You’re a genius!!! It works! One more question, is it possible to display an object (in this case a text) if an another object.y is greater than the screen.y?

Thanks [import]uid: 76800 topic_id: 25682 reply_id: 103852[/import]

Hah, glad to hear it.

RE the other question, yes, certainly - this is a random example;
[lua]local circle = display.newCircle( 160, 200, 30 )

local function moveCircle()
transition.to(circle, {x=math.random(-200,520), y=math.random(-200,620), onComplete=moveCircle})
end
moveCircle()

local statusText = display.newText("", 80, 10, native.systemFont, 18)

local function adjustText()
if circle.x > 320 or circle.x < 0 then
statusText.text = “Outside”
elseif circle.y > 480 or circle.y < 0 then
statusText.text = “Outside”
else
statusText.text = “Inside”
end
end
Runtime:addEventListener(“enterFrame”, adjustText)[/lua]

There would likely be better ways of handling it though, depending on what is moving outside the screen and how it is getting there but that might give you something to go from.

Peach :slight_smile: [import]uid: 52491 topic_id: 25682 reply_id: 103857[/import]

Once again, it works! Thanks! [import]uid: 76800 topic_id: 25682 reply_id: 103866[/import]

Not a problem, good luck with your project :slight_smile: [import]uid: 52491 topic_id: 25682 reply_id: 103880[/import]