I’m having some trouble with removing lines that the user has drawn.
I’m using this code for the drawing: Draw a line
[lua]-- Starts physics
local physics = require (“physics”)
physics.start(true)
physics.setGravity(10, 0)
– Ball rolls on Line
local ball = display.newCircle( 0, 0, 25)
ball:setFillColor(0, 255, 0)
ball.x = display.contentHeight/12
ball.y = display.contentWidth/4
physics.addBody(ball, {bounce=0.3, radius = 25, friction=0.5})
– Draw a line
local i = 1
local tempLine
local rectangle_hit = {}
local prevX , prevY
local function runTouch(event)
if(event.phase==“began”) then
if(tempLine==nil) then
tempLine=display.newLine(event.x, event.y, event.x, event.y)
– Random Colors for line
r = math.random (0, 255)
g = math.random ( 0, 255)
b = math.random (0, 255 )
tempLine:setColor(r,g, b)
prevX = event.x
prevY = event.y
end
elseif(event.phase==“moved”) then
tempLine:append(event.x,event.y-2)
tempLine.width=tempLine.width+0.9
rectangle_hit[i] = display.newLine(prevX, prevY, event.x, event.y)
rectangle_hit[i]:setColor(r,g, b)
rectangle_hit[i].width = 5
– Creates physic joints for line (Turn on draw mode to see the effects)
local Width = rectangle_hit[i].width * 0.6
local Height = rectangle_hit[i].height * 0.2
– Physic body for the line shape
local lineShape = {-Width,-Height,Width,-Height,Width,Height,-Width,Height}
physics.addBody(rectangle_hit[i], “static”, { bounce = -1, density=0.3, friction=0.7, shape = lineShape})
prevX = event.x
prevY = event.y
i = i + 1
elseif(event.phase==“ended”) then
tempLine.parent.remove(tempLine)
tempLine=nil
end
end
Runtime:addEventListener(“touch”, runTouch)[/lua]
Then, I’ve also got a refresh button that should make all the lines disappear.
The lines live in the “rectangle_hit” array, so I thought the following loop would get rid of all of them, but when it runs, I just get an error that says, “Attempt to index field ‘?’ (a nil value” and a reference to the removeSelf line in the following loop.
[lua]local function clearLines(event)
for a=0, table.getn(rectangle_hit), 1 do
rectangle_hit[a]:removeSelf()
end
end[/lua]
Any idea how to get this to actually work?
[import]uid: 78380 topic_id: 12892 reply_id: 312892[/import]

[import]uid: 23689 topic_id: 12892 reply_id: 47591[/import]