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".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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

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:

1
2
3
4
5
6
local function removeAllLines()
    for i=#myLines,1,-1 do
        myLines[i]:removeSelf()
        myLines[i] = nil           
    end
end    

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!

views:1507 update:2011/9/26 15:43:22
corona forums © 2003-2011