Lives System

So i have my variables for lives, my code for it to display and how i want it to go down but it doesnt manage to go down.

Heres the code thats driving me insane

[lua]local lives = 55

local livesTEXT = display.newText("Live: " … lives, display.contentWidth - 150, 20, nil, 26)
livesTEXT:setTextColor(0,0,255)

if balloon.y <= 0 then
lives = lives - 1
print("LIVES LEFT: "… lives)

livesTEXT.text = "Live: "… lives
end [/lua]

the balloon variable is located somewhere else in my script and it has physics to where it flys upward but whenever i change the balloon.y >= 0 than it works but obviously not the way i want it but that lets me know i dont have any errors in my code but once if its set up the way it should be the lives variable doesnt subtract, doesnt show in the console doesnt do anything…

All help is appreciated,
Brennan Vargas [import]uid: 81383 topic_id: 18589 reply_id: 318589[/import]

So if you change “balloon.y <= 0” to “balloon.y >= 0” it works? If that is the case, then whatever is causing your problem is not in the block of code you posted. To me that means the balloon’s y position is never going below zero (which by default would be above the top of the screen). The best thing I can think of for you to do is try tracking the y position so you can get a better idea of what the balloon’s actual coordinates are. Here is some code that should do that:

local function checkFrame(event)  
 print("BALLOON Y:",balloon.y)  
end  
   
Runtime:addEventListener("enterFrame",checkFrame)  

Hope that helps. [import]uid: 94868 topic_id: 18589 reply_id: 71430[/import]

Thanks but i decided to change it up a bit since that was having a problem. How could i make it delete on collide? I have a collision set up, most likely not properly, but once the balloon collides with the spikey ceiling it subtracts a life -1 and i have it set up to where it is suppose to delete a balloon but the balloon doesnt delete even though there in the same function. But if i dont pop any when the first balloon hits the ceiling all the currently showing balloons do pop.

How can i fix this?

Heres My Code

[lua]system.activate(“multitouch”)

local physics = require(“physics”)
physics.start()

physics.setGravity(0, -6.8)
local pop = media.newEventSound( “balloonpop.wav” )

local background = display.newImage(“Graphics/bricks.png”)

local leftWALL = display.newRect(0, 0, 1, display.contentHeight + 500 )
local rightWALL = display.newRect(display.contentWidth, 0, 1, display.contentHeight + 500 )
local ceiling = display.newImage(“Graphics/spike.png”)

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.newImage(“Graphics/balloon1.png”)
balloon.x = display.contentWidth/2
balloon.y = display.contentHeight + 150
physics.addBody(balloon, {radius=50})

function deadBalloon(event)
if event.phase == “ended” then

lives = lives - 1
print("LIVES LEFT: "… lives)

livesTEXT.text = “LIVES:” … lives
balloon:removeSelf()

end
end

function killBalloon(event)
if event.phase == “ended” then

score = score + 1
print("SCORE IS: "… score)

scoreTEXT.text = “You Popped " … score … " Balloons”

media.playEventSound( pop )

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

ceiling.collision = deadBalloon
ceiling:addEventListener( “collision”, deadBalloon )

balloon:addEventListener(“touch”, killBalloon)

end

local function nukeB()

end

local function LiveSystem()
if lives <= 0 then
director:changeScene(“index”)
end
end

timer.performWithDelay( 250, spawnBalloon, 100)
display.setStatusBar( display.HiddenStatusBar )[/lua]

Thanks in Advanced,
Brennan [import]uid: 81383 topic_id: 18589 reply_id: 71442[/import]

Your collision function is not set up properly,you need to declate with what object it will collide [import]uid: 16142 topic_id: 18589 reply_id: 71443[/import]

sorry im new, could you give an example? [import]uid: 81383 topic_id: 18589 reply_id: 71445[/import]

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.

  1. So you only need to make this collision listener once. So put the listener and the deadballoon function outside of the spawnBalloon function

  2. 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]