Hello,
I have a button that you press to make a line disappear, which you can see on the below code. It works fine the first time you swipe over the simulator and it produces a line, once you press the button the line vanishes. The only problem is that it only works once, if you try it again it has some sort of “bug” a line out of know where appears with no physic body. But the real problem is that you can’t delete the line the second time and i can’t figure out why. It is really weird, if you try it out you will see that the second time everything goes wrong. I just want to be able to draw a line constantly and press the button and the line deletes it self. Can anyone help me by giving advice or please an example of what is wrong. Thanks so much!
- All you need to have to test it out is one image for the UI button.
Thanks!
The code:
[code]
local ui = require(“ui”)
– THE LINE IS A PHYSIC BODY, YOU CAN SEE IF YOU TURN ON DRAW MODE. THANKS.
physics = require (“physics”)
physics.start()
–physics.setDrawMode “hybrid”
– Draw a line, swipe the simulator.
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)
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(105, 105, 105)
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 )
– Delete the line code:
for i = 1, #rectangle_hit, 1 do
rectangle_hit[i]:removeSelf()
rectangle_hit[i] = nil
end
end
– UI button, change the image to yours.
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: 14079 reply_id: 314079[/import]