I am trying to have a Nuke Balloon remove all balloons on the screen. Currently whenever I run this function it returns nil for the balloons
local function RemoveAllBalloons() for i=1, #balloons do print(balloons[i].MyName) end end
Therefore whenever I change the print to balloons[i]:removeSelf() of course it is going to result in an error.
Whenever i spawn a balloon this is the code that I use
local function SpawnBalloon() local balloon = display.newImageRect( "Balloon.png", 72, 72 ) balloon.y = display.contentHeight + 150 balloon.rotation = 0 balloon.MyName = "Balloon" balloon.score = 1; balloon.positioninballons = #balloons+1 balloon.x = math.random(30, display.contentWidth - 30) group:insert( balloon ) table.insert( balloons, balloon.positioninballons, balloon ) physics.addBody( balloon, { density=1.0, friction=0.3, bounce=0.3 } ) local function touchHandler( event ) balloon:removeSelf() table.remove(balloons, positioninballons) myData.score = myData.score + 1 PlayerScore.text = "Score: " .. myData.score end balloon:addEventListener("touch", touchHandler) end
Spawns the balloon no problem and everything works fine. Now when it comes to the nuke balloon the touchHandler function is a bit different
local function touchHandler( event ) balloon:removeSelf() table.remove(balloons, positioninballons) myData.score = myData.score + 1 PlayerScore.text = "Score: " .. myData.score transition.to(flash, {alpha=1, time=50}) RemoveAllBalloons() timer.performWithDelay(100, ResetFlash, 1) end balloon:addEventListener("touch", touchHandler)
So I am pretty sure that I have everything written correctly, as in syntax that is. There is just a problem when it comes to removing them or even printing them. I have no idea why every single table index is coming back as nil and if anyone can explain to me why that would be much appreciated.