How to erase a drawn line

I have function (drawLine) that allows the user to draw a line on the screen with their finger. However, when using Storyboard to change the scenes, I cannot get the line to disappear off the screen. Usually all that is needed is to add the objects in the drawLine function to the Storyboard group and Storyboard will delete it automatically. But this function has a table that inserts the objects into the table and I cannot insert the objects into the group. Take a look here, I have commented out where I have tried unsuccessfully to add objects from the drawLine function. Any advise on how to add the drawLine objects to the Storyboard group will be much appreciated!
[lua]local lineTable = {}

– This is a required table that will contain each drawn line as a separate display group, for easy referral and removal
local lineWidth = 3

local lineColor = {R=math.random(0,255), G=math.random(0,255), B=math.random(0,255)}

local newLine = function(event)

local function drawLine()
local line = display.newLine(linePoints[#linePoints-1].x,linePoints[#linePoints-1].y,linePoints[#linePoints].x,linePoints[#linePoints].y)
line:setColor(lineColor.R, lineColor.G, lineColor.B);
line.width=lineWidth
lineTable[i]:insert(line)
–group:insert(line)
local circle = display.newCircle(linePoints[#linePoints].x,linePoints[#linePoints].y,3)
circle:setFillColor(lineColor.R, lineColor.G, lineColor.B)
physics.addBody(circle, “static”,{bounce = 0.1, friction = .7} )
lineTable[i]:insert(circle)
–group:insert(circle)
end

if event.phase==“began” then
i = #lineTable+1
lineTable[i]=display.newGroup()
display.getCurrentStage():setFocus(event.target)

circle = display.newCircle(event.x,event.y,lineWidth/2)
circle:setFillColor(lineColor.R, lineColor.G, lineColor.B)
lineTable[i]:insert(circle)
–group:insert(lineTable[i])
linePoints = nil
linePoints = {};

local pt = {}
pt.x = event.x;
pt.y = event.y;
table.insert(linePoints,pt);

elseif event.phase==“moved” then
local pt = {}
pt.x = event.x;
pt.y = event.y;
if not (pt.x==linePoints[#linePoints].x and pt.y==linePoints[#linePoints].y) then
table.insert(linePoints,pt)
drawLine()
end

elseif event.phase==“cancelled” or “ended” then
display.getCurrentStage():setFocus(nil)
i=nil
end

return true
end
Runtime:addEventListener(“touch”,newLine) [import]uid: 75779 topic_id: 32470 reply_id: 332470[/import]

Have you defined ‘group’ anywhere?
[import]uid: 135765 topic_id: 32470 reply_id: 129143[/import]

Just insert linePoints into your Storyboard group, it will work fine. I do exactly the same thing.

Dave [import]uid: 117617 topic_id: 32470 reply_id: 129146[/import]

@ r.delia yes Storyboard at the beginning of the enterScene event puts everything into a group with local group = self.view so usually you have to insert all your objects into the group.

@ thedavebaxter where exactly do I put group:insert(linePoints)? (I am getting errors putting it i the “moved” phase) [import]uid: 75779 topic_id: 32470 reply_id: 129151[/import]

Stick it in CreateScene.

Dave [import]uid: 117617 topic_id: 32470 reply_id: 129159[/import]

Have you defined ‘group’ anywhere?
[import]uid: 135765 topic_id: 32470 reply_id: 129143[/import]

Just insert linePoints into your Storyboard group, it will work fine. I do exactly the same thing.

Dave [import]uid: 117617 topic_id: 32470 reply_id: 129146[/import]

@ r.delia yes Storyboard at the beginning of the enterScene event puts everything into a group with local group = self.view so usually you have to insert all your objects into the group.

@ thedavebaxter where exactly do I put group:insert(linePoints)? (I am getting errors putting it i the “moved” phase) [import]uid: 75779 topic_id: 32470 reply_id: 129151[/import]

Stick it in CreateScene.

Dave [import]uid: 117617 topic_id: 32470 reply_id: 129159[/import]

@ Dave

I tried putting “group:insert(linePoints)” in the createScene instead of enterScene and now I’m getting error:
ERROR: table expected. If this is a function call, you might have used ‘.’ instead of ‘:’

What do you think I’m doing wrong? You are using the same drawing function also in Storyboard?

thanks [import]uid: 75779 topic_id: 32470 reply_id: 129235[/import]

Sorry my fault, just checked my code.

I create a local group at the top of my scene.lua (so all functions can access it). This group is inserted into the StoryBoard group in createScene -

group:insert(characterGroup)

Then in on line 19 and 26 above (where you do group:insert, change to -

characterGroup:insert( lineTable[i] )

Dave
[import]uid: 117617 topic_id: 32470 reply_id: 129237[/import]

Okay not there yet sorry. I did what you explained but not sure what to put at top of scene.lua, I tried
local characterGroup = display.newGroup() but then that will give me an error on line 19 characterGroup:insert(lineTable[i]) “attempt to call method ‘insert’ (a nil value)”

Maybe its something with when you say “so all functions can access it” that I am missing? [import]uid: 75779 topic_id: 32470 reply_id: 129245[/import]

I can’t work out why it isn’t working, that is exactly what I do.

“So all functions can access it” just means if you put it outside of all functions (usually at the top) then all the functions in that scene have access to it.

Try taking the local off it or maybe post your whole scene.lua file or email it me (thedavebaxter @ gmail.com)

EDIT: and yes am using the exact same library.

Dave [import]uid: 117617 topic_id: 32470 reply_id: 129257[/import]

@ Dave

I tried putting “group:insert(linePoints)” in the createScene instead of enterScene and now I’m getting error:
ERROR: table expected. If this is a function call, you might have used ‘.’ instead of ‘:’

What do you think I’m doing wrong? You are using the same drawing function also in Storyboard?

thanks [import]uid: 75779 topic_id: 32470 reply_id: 129235[/import]

Sorry my fault, just checked my code.

I create a local group at the top of my scene.lua (so all functions can access it). This group is inserted into the StoryBoard group in createScene -

group:insert(characterGroup)

Then in on line 19 and 26 above (where you do group:insert, change to -

characterGroup:insert( lineTable[i] )

Dave
[import]uid: 117617 topic_id: 32470 reply_id: 129237[/import]

Okay not there yet sorry. I did what you explained but not sure what to put at top of scene.lua, I tried
local characterGroup = display.newGroup() but then that will give me an error on line 19 characterGroup:insert(lineTable[i]) “attempt to call method ‘insert’ (a nil value)”

Maybe its something with when you say “so all functions can access it” that I am missing? [import]uid: 75779 topic_id: 32470 reply_id: 129245[/import]

I can’t work out why it isn’t working, that is exactly what I do.

“So all functions can access it” just means if you put it outside of all functions (usually at the top) then all the functions in that scene have access to it.

Try taking the local off it or maybe post your whole scene.lua file or email it me (thedavebaxter @ gmail.com)

EDIT: and yes am using the exact same library.

Dave [import]uid: 117617 topic_id: 32470 reply_id: 129257[/import]