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]