I tried out the code.
The collision listener for the ceiling. You have it in the spawnBalloon function. So every time you make a balloon, you create another listener for the ceiling. And this seems to be the source of why all of the balloons are disappearing with one collision. Every time there is any collision all of the deadballoon functions are called. And since it is a direct reference to each balloon (there is nothing specifying which balloon actually collided) all of the balloons are removed.
-
So you only need to make this collision listener once. So put the listener and the deadballoon function outside of the spawnBalloon function
-
And to specify the specifc balloon that is involved with the collision. Since you are making it a table listener, make sure you add the “self” as the first parameter of the listener function, so that the “event” parameter is actually passed to function when it is called. Then with this local collision function you can access the other object in the collision with event.other.
The code below should work and reflects these changes (i either deleted or replaced your specific images with newRect objects):
[code]
system.activate(“multitouch”)
local physics = require(“physics”)
physics.start()
physics.setGravity(0, -6.8)
local leftWALL = display.newRect(0, 0, 1, display.contentHeight + 500 )
local rightWALL = display.newRect(display.contentWidth, 0, 1, display.contentHeight + 500 )
local ceiling = display.newRect(0,0,10,20)
ceiling.x = display.contentWidth/2
physics.addBody(leftWALL, “static”)
physics.addBody(rightWALL, “static”)
physics.addBody(ceiling, “static”)
local score = 0
local scoreTEXT = display.newText(“You Popped " … score … " Balloons”, 20, 20, nil, 36)
scoreTEXT:setTextColor(255,255,255)
local lives = 50
local livesTEXT = display.newText("Live: " … lives, 20, display.contentHeight - 50, nil, 36)
livesTEXT:setTextColor(0,255,0)
local function spawnBalloon()
local balloon = display.newCircle(0,0,30)
balloon.x = display.contentWidth/2
balloon.y = display.contentHeight + 150
physics.addBody(balloon, {radius=50})
balloon.name = “balloon”
local function killBalloon(event)
if event.phase == “ended” then
score = score + 1
print("SCORE IS: "… score)
scoreTEXT.text = “You Popped " … score … " Balloons”
local text = display.newText(“You Popped Another Balloon!”, 50, 100, nil, 26)
text:setTextColor(0,0,0)
transition.to(text, {time = 2000, alpha = 0, x = 300, y = 400})
balloon:removeSelf( )
end
end
if score == 20 then
local nukeballoon = display.newImage(“Graphics/nukeBALLOON.png”)
nukeballoon.x = display.contentWidth/2
nukeballoon.y = display.contentHeight + 150
physics.addBody(nukeballoon)
function killnBalloon(event)
if event.phase == “ended” then
lives = lives - 20
print("LIVES LEFT: "… lives)
livesTEXT.text = "Live: "… lives
nukeballoon:removeSelf( )
end
end
nukeballoon:addEventListener(“touch”, killnBalloon)
end
if score == 60 then
local nukeballoon = display.newImage(“Graphics/nukeBALLOON.png”)
nukeballoon.x = display.contentWidth/2
nukeballoon.y = display.contentHeight + 150
physics.addBody(nukeballoon)
function killnBalloon(event)
if event.phase == “ended” then
lives = lives - 20
print("LIVES LEFT: "… lives)
livesTEXT.text = "Live: "… lives
nukeballoon:removeSelf( )
end
end
nukeballoon:addEventListener(“touch”, killnBalloon)
end
if score >= 100 then
local nukeballoon = display.newImage(“Graphics/nukeBALLOON.png”)
nukeballoon.x = display.contentWidth/2
nukeballoon.y = display.contentHeight + 150
physics.addBody(nukeballoon)
function killnBalloon(event)
if event.phase == “ended” then
lives = lives - 20
print("LIVES LEFT: "… lives)
livesTEXT.text = "Live: "… lives
nukeballoon:removeSelf( )
end
end
nukeballoon:addEventListener(“touch”, killnBalloon)
end
balloon:addEventListener(“touch”, killBalloon)
end
local function nukeB()
end
local function deadBalloon(self,event)
local poppedBalloon = event.other
if event.phase == “ended” then
lives = lives - 1
print("LIVES LEFT: "… lives)
livesTEXT.text = “LIVES:” … lives
poppedBalloon:removeSelf()
end
end
ceiling.collision = deadBalloon
ceiling:addEventListener( “collision”, ceiling )
timer.performWithDelay( 250, spawnBalloon, 100)
display.setStatusBar( display.HiddenStatusBar )
[/code] [import]uid: 94868 topic_id: 18589 reply_id: 71448[/import]