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:
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 76 77 | 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 |
every time you draw a line its handler is rectangle_hit itself. rectangle_hit is a table which actually stores many small lines which make the line you draw. so once you delete it in a loop it will delete the lines inside the table. so next time when u draw u use the same handle. so deleting is not that easy unless you have some means to identify the drawn lines separately.
look at the following code to see how you can create a handle for each line you draw.
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 | local lines = {} local myLines = {} local prevX,prevY 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 myLines[i][#myLines[i] + 1] = display.newLine(prevX,prevY,e.x,e.y) myLines[i][#myLines[i]].width = 3 end prevX = e.x prevY = e.y elseif "ended" == e.phase then prevX = nil prevY = nil i = i + 1 --you can get to individual lines using this. removeLine(myLines[#myLines-1]) end end Runtime:addEventListener("touch",drawLine) |
Hey,
Thanks a lot! I don't have that much time to look over it but it I scanned through it and sounds familiar to what i was thinking. Thanks for it! Another thing i was wondering about very similar to the deleting the lines is to just undo a bit of it, basically deleting very small bits of the line by pressing the button. Instead of removing each one, just deleting little bits of it until theres none left. Thanks a lot for the code that you gave me.
Michael