Clear All Lines

Hello,

I’ve been trying to figure this out for a very long time and thanks to Peach, helped me to add physics to this line. But what i was trying to accomplish is having a button that you press, which then you would have already drew lines all over the screen. Once you press the button all the lines on the screen should vanish, but unfortunately it did not go as I planned… Once you press the button it deletes the line that you previously drew. What I really wanted was when you press the button, it deletes ALL the lines wich it currently isn’t doing right now. Could anyone help to modify this so i can delete all the lines? Not just the previous ones…? Thanks so much.

Please take time to test it out and you will get the real feel of it:

-All you need to do to fully test it out is add one image for the button, replace “drop.png”.

[code]

require “ui”
require “physics”
physics.start()
physics.setDrawMode “hybrid”

– //COMMENT//: Tables.

local myLines = {}
local prevX,prevY

– //COMMENT//: Function to clear previous Line.

local function removeLine(myLine)
for i=1,#myLine do
myLine[i]:removeSelf()
myLine[i] = nil

end
end

local i = 1
local function drawLine(e)
if “began” == e.phase then
myLines[i] = {}
prevX = e.x
prevY = e.y
elseif “moved” == e.phase then
if prevX then

– //COMMENT//: This is important. It tells “myLines” to add 1, on the removeLine function it then tells to subtract 1.
myLines[i][#myLines[i] + 1] = display.newLine(prevX,prevY,e.x,e.y)
myLines[i][#myLines[i]].width = 4
myLines[i][#myLines[i]]:setColor(105, 105, 105)

– //COMMENT//: Physic body for the line shape. Make physic body bigger/smaller on the line.
local Width = myLines[i][#myLines[i]].width * 1
local Height = myLines[i][#myLines[i]].height * 0.1

– //COMMENT//: LineShape is the shape of the square static bodies, I added “shape = lineShape” in the physics below.
local lineShape = {-Width,-Height,Width,-Height,Width,Height,-Width,Height}

– //COMMENT//: Add physic body for shape with static squares.
physics.addBody(myLines[i][#myLines[i]], “static”, {density = 1.0, friction = 0.3, bounce = 0.2, shape = lineShape})
end
prevX = e.x
prevY = e.y
elseif “ended” == e.phase then
prevX = nil
prevY = nil
i = i + 1
end
end
Runtime:addEventListener(“touch”,drawLine)

local button1Press = function( event )

– //COMMENT//: This calls the function above, to erase the line that you previously drew.
removeLine(myLines[#myLines-1])

end

– //COMMENT//: Add a new Image for “drop.png”, to fit your needs.

local hi = ui.newButton{
default = “drop.png”,
over = “drop.png”,
onPress = button1Press,
emboss = true
}

hi.x = display.contentWidth/2
hi.y = display.contentHeight/2
[import]uid: 66985 topic_id: 14411 reply_id: 314411[/import]

Hey there,

From my understanding you’d simply like to remove all lines drawn when the user presses the “drop” button.

Your “removeLine(myLine)” (line 13) function is simply removing the ONE line you passed it in the line “removeLine(myLines[#myLines-1])” (line 61), and not ALL lines.

To solve this, replace your “removeLine(myLine)” (line 13) function with the following function:

[lua]local function removeAllLines()
for i=#myLines,1,-1 do
myLines[i]:removeSelf()
myLines[i] = nil
end
end [/lua]

The previous function iterates through all lines in your “myLines” table removing them one by one.

And now replace “removeLine(myLines[#myLines-1])” (line 61) with the following line:

[lua]removeAllLines()[/lua]

That should do it!

Regards,

Andreas Ricci
NuPlay Entertainment
Founder & Lead Developer [import]uid: 7366 topic_id: 14411 reply_id: 53319[/import]

It seems still not to work! Thanks for your your advice, but no success… I want exactly what you said, to remove all the lines. Do you have any other ideas? Thanks a lot! [import]uid: 66985 topic_id: 14411 reply_id: 53396[/import]