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:
[code]
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
[import]uid: 23689 topic_id: 13797 reply_id: 313797[/import]