Clear Button

Hi,

I have a clear button, you draw a line and press then press the clear button. It removes the line the first time, the second time it "bugs up", it does not remove the line and has a major long line out of nowhere. Try out the code below and you will see the problem, the first time you swipe on the simulator and produce the line you tap the button and it works. The second time, it fails. Got any ideas? Anyone know why?

Thanks.

The code:

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
local ui = require("ui")
 
 physics = require ("physics")
physics.start()
--physics.setDrawMode "hybrid"
physics.setGravity(0, 20)
 
-- 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
tempLine.alpha = 0
 
-- 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)
 
 
local button1Press = function( event )
for i = 1, #rectangle_hit, 1 do
              rectangle_hit[i]:removeSelf()
              rectangle_hit[i] = nil
              
 
end
end
 
local hi = ui.newButton{
        default = "drop.png",
        over = "drop.png",
        onPress = button1Press,
        emboss = true
}
 
hi.x = display.contentWidth/14
hi.y = display.contentHeight/12

Try this :

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
local ui = require("ui")
 
physics = require ("physics")
physics.start()
--physics.setDrawMode "hybrid"
physics.setGravity(0, 20)
 
-- 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
tempLine.alpha = 0
 
-- 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)
 
local button1Press = function( event )
for i = 1, #rectangle_hit, 1 do
if rectangle_hit[i].removeSelf then --Added
    rectangle_hit[i]:removeSelf()
    rectangle_hit[i] = nil
end
rectangle_hit = {} --Added
 
end
end
 
local hi = ui.newButton{
default = "drop.png",
over = "drop.png",
onPress = button1Press,
emboss = true
}
 
hi.x = display.contentWidth/14
hi.y = display.contentHeight/12

Thanks for your response! I tried it though and still no success... The bug is still there, the line doesn't erase either. Do you have any other ideas?

Thanks a lot.

views:1246 update:2011/9/27 8:54:05
corona forums © 2003-2011