I am using removeSelf() of a number of display objects, and it works properly, but gives me this error shown at the bottom of 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 | local dug = display.newImage("images/dugOut.png", 0, 0) --physics.addBody( dug, "static", { friction=0.6 } ) dug.alpha = 0.5254901960784314 dug.collisionType = "dug" dug.timesHit = 0 dug.points = 0 hbLeft = display.newImage("images/hitBorder.png", 120, 0) physics.addBody( hbLeft, "static", { friction=0.6 } ) hbLeft.collisionType = "dug" hb1 = display.newImage("images/hitBorder.png", 28, 0) physics.addBody( hb1, "static", { friction=0.6 } ) hb1.collisionType = "dug" hb2 = display.newImage("images/hitBorder.png", 120, 0) physics.addBody( hb2, "static", { friction=0.6 } ) hb2.collisionType = "dug" hb3 = display.newImage("images/hitBorder.png", 360, 0) physics.addBody( hb3, "static", { friction=0.6 } ) hb3.collisionType = "dug" hb4 = display.newImage("images/hitBorder.png", 450, 0) physics.addBody( hb4, "static", { friction=0.6 } ) hb4.collisionType = "dug" local function removeSetup() dug:removeSelf() hb1:removeSelf() hb2:removeSelf() hb3:removeSelf() hb4:removeSelf() buttonSmall:removeSelf() end buttonHandler = function( event ) --t:setText( "id = " .. event.id .. ", phase = " .. event.phase ) timer.performWithDelay(1000, removeSetup) --dug:removeSelf() --hb1:removeSelf() --hb2:removeSelf() --hb3:removeSelf() --hb4:removeSelf() --t = event.target --t:removeSelf() end buttonSmall = ui.newButton{ default = "images/buttonBlueSmall.png", over = "images/buttonBlueSmallOver.png", onEvent = buttonHandler, id = "smallBtn", text = "Fight", size = 12, emboss=true } buttonSmall.x = 240; buttonSmall.y = 160 |
I ran into the same problem. Never really figured out why it was happening, but my assumption is that it was trying to remove itself twice, so the second time it was already a nil value. This is what I ended up using:
1 2 3 4 5 6 7 8 9 10 11 12 13 | local function remove( display ) return display:removeSelf() end local object = display.newImage("myImage.png") local itemWorked, result = pcall(remove, object) -- pcall is a native lua method, Corona supports it if itemWorked then print(result) -- true else print(result) -- false, this fires if it fails to remove end |
put a print(event.phase)
statement in your button handler. my guess is it's firing more than once, ie for the "began" and the "ended" phases. since you're not checking which, it'll remove everything in the first phase, and fail when it tries to remove them in the second because the objects have already been removed
When you have a set of objects you want to dynamically remove, put them in a table. Then when you need to remove them, you can just loop through the table and then reset the table. This will keep you from having this error message because it won't try to remove objects that were previously removed even if you call it twice in a row. You can also easily check to see if any items in the table exist by checking the table.maxn value.