Removing drawn lines

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]

Your “rectangle_hit” array will start at 1, not 0. Try this:

  
local function clearLines(event)  
 for i = 1, #rectangle\_hit, 1 do  
 rectangle\_hit[i]:removeSelf()  
 rectangle\_hit[i] = nil  
 end  
end  
  

Haven’t tested that so can’t guarantee anything. [import]uid: 5833 topic_id: 12892 reply_id: 47329[/import]

Thanks Graham, that works pretty well.

It gets rid of the physics body and most of the line.

However, now I’m left with some sort of “residual”

Here’s a screenshot.

I thought it had something to do with the tempLine, but that’s nil’d out as well.

Any thoughts? Maybe it’s just the emulator? [import]uid: 78380 topic_id: 12892 reply_id: 47332[/import]

Hi

In line 26, you seem to draw pixels(not lines) The points you draw do not seem to be added to your table rectangle hit. Maybe that is why some points get left behind. The points also are set with the event.x and event.y which i believe are somewhat noisy. Just my guess…
Good luck.

Mo [import]uid: 49236 topic_id: 12892 reply_id: 47344[/import]

I had a prob with this too :wink: [import]uid: 23689 topic_id: 12892 reply_id: 47591[/import]

Can i see all your code please? [import]uid: 23689 topic_id: 12892 reply_id: 48231[/import]