Need to delete drawn Line Group...

Hello! I'm some what new to Corona and Lua (spent most of my time using Unity and UnityScript). In my code, I'm having a player draw a line of a certain length and then, if he or she wishes, they can delete any and all lines and then try again by using a button press. The issue is I cannot delete the group (nil) and have the player redraw without an error once it is gone. The code should be stored in the button function, which I've deleted the meat of it. Any help?! display.setStatusBar( display.HiddenStatusBar ) local physics = require "physics" physics.start() local widget = require( "widget" ) local w = display.contentWidth local h = display.contentHeight local lines = {} local lineGroup = display.newGroup() local prevX,prevY local isDrawing = false local i = 0 local kittenCrate = display.newRect(10,10,25,25) physics.addBody(kittenCrate, "dynamic", { density = 1, friction = 0.5, bounce = 1.6}) 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 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 \< 100) then if(lines[i]) then lineGroup:remove(i) end lines[i] = display.newLine(prevX, prevY, e.x, e.y) lines[i]:setColor(255, 255, 0) lines[i].width = 5 local dist\_x = e.x - prevX local dist\_y = e.y - prevY physics.addBody(lines[i], "static", { density = 1, friction = 0.5, bounce = 1, 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) local function handleButtonEvent( event ) if ( "ended" == event.phase ) then print( "Button was pressed and released" ) end end -- Create the widget local button1 = widget.newButton { left = 100, top = 200, id = "button1", label = "Remove Rifts", onEvent = handleButtonEvent }

lineGroup:removeSelf()?

Thanks for the help.  This works fine until I attempt to redraw.

It gives me an insert(a nil value) on line 44.  I’m assuming that’s because we removed the lineGroup so it has nothing to insert into.  Do we have to removeSelf or remove the (i)  Which also gives me an error.

Any more ideas, Rob?

Thanks again!

You can loop through the group, removing each child.  I don’t have that code off of the top of my head.  The other thing would be to recreate the group immediately after you remove it.

I attempted to call another local lineGroup immediatly after lineGroup : removeSelf().  That didn’t do it.  I also attempted to call it after if(e.phase == “began”) in the drawLine(e) function.  Is there a more appropriate place to recreate the group?

Thanks again for the quick reply!!

lineGroup:removeSelf()?

Thanks for the help.  This works fine until I attempt to redraw.

It gives me an insert(a nil value) on line 44.  I’m assuming that’s because we removed the lineGroup so it has nothing to insert into.  Do we have to removeSelf or remove the (i)  Which also gives me an error.

Any more ideas, Rob?

Thanks again!

You can loop through the group, removing each child.  I don’t have that code off of the top of my head.  The other thing would be to recreate the group immediately after you remove it.

I attempted to call another local lineGroup immediatly after lineGroup : removeSelf().  That didn’t do it.  I also attempted to call it after if(e.phase == “began”) in the drawLine(e) function.  Is there a more appropriate place to recreate the group?

Thanks again for the quick reply!!