Win event and lose event happening at the same time

OK let’s say you have a game like when you throw a ball at pins. You are allowed to have three balls and you have three pins. If you hit the last pin with the last ball you need your game to be won. But you used your last ball so at the same time the game is saying that you lost. Does anyone have any ideas about how you could throw the last ball and not have game over until you know the ball didn’t hit the last pin? [import]uid: 49863 topic_id: 30306 reply_id: 330306[/import]

This is only an ide, but dont dispatch an event when the ball gets thrown, instead check how many balls are left and how many pins are left, maybe with a timer. If ball == 0 and pins > 0 then dispatch the lose event otherwise the won event. Since you havent posted any code its hard to help to pinpoint exactly what you can change. [import]uid: 126729 topic_id: 30306 reply_id: 121406[/import]

I would have post a lot of code for this. I tried the if ball == 0 and pins > 0 the game ends while ball is still being thrown. So in the time it takes the last ball to hit the last pin the lose event starts and once the last pin is hit the next level loads. [import]uid: 49863 topic_id: 30306 reply_id: 121410[/import]

Thats why you should delay the event. You know when the last ball is being thrown, then start a timer for a second or how long it now takes, and then call a function who checkes ball vs pins.
[import]uid: 126729 topic_id: 30306 reply_id: 121412[/import]

Not a bad idea I will try it out. Thanks for the suggestion. [import]uid: 49863 topic_id: 30306 reply_id: 121413[/import]

Well I wasn’t able to figure out a way to make this work. Here is the code maybe it will trigger a light bulb for someone who already knows how to do this. I just picked out the parts I thought were necessary. If something seems to be missing let me know and I will include that as well. Thanks

[lua]-------------in the update function-------

if dartsLeft == 2 then

dart4.isVisible = false
gameState = “removing balloons”

elseif dartsLeft == 1 then

dart3.isVisible = false
gameState = “removing balloons”

elseif dartsLeft == 0 then

dart2.isVisible = false
gameState = “removing balloons”

end

if balloons.numChildren == 0 and gameState == “removing balloons” then

if dart4.isVisible == true then
dart4.isVisible = false
end
if dart3.isVisible == true then
dart3.isVisible = false
end
if dart2.isVisible == true then
dart2.isVisible = false
end
dartsLeft = 0

end

if dartsLeft == 0 and balloons.numChildren ~= 0 then

gameState = “over”

end

-------------------------------the following happens when the player beats a level
– we start building the next level

function changeLevel(event)

if balloons.numChildren == 0 and gameState == “removing balloons” and table.maxn(levels) ~= currentLevel and gameState ~= “lost” then

timer.performWithDelay(2500, createLevel, 1)
Runtime:removeEventListener( “enterFrame”, changeLevel)
gameExitBtn:removeEventListener(“touch”, removeListeners)

end

end

------------------------------------function for losing
function lost(event)

if balloons.numChildren ~= nil then

if gameState == “over” then

if dartsLeft == 0 and balloons.numChildren > 0 then

Runtime:removeEventListener(“enterFrame”, lost)
gameLosing()

end

end

end

end

local gameLosing = function (event)

gameState = “lost2”
btPopUpt()

end

[import]uid: 49863 topic_id: 30306 reply_id: 121533[/import]

I guess you are trying to make the update in the enterFrame listener. If i would try to do this i would do it through a touch listener. I guess you have a touch listener already for making the throws.

Then try something like this:
[lua]local function checkGameState()
if (dartsLeft == 0 and ballonsLeft == 0 ) then
–Call Win
else
–Call Lose
end
end

local function throwDart()
dartsLeft = dartsLeft -1
–Rest of the darts visible checks
if (dartsLeft == 0) then
timer.PerformWithDelay(1000, checkGameState)
end
end

background:addEventListener(“touch”, throwDart) – add the listener to the object you are throwing/touching[/lua]

[import]uid: 126729 topic_id: 30306 reply_id: 121537[/import]

OK that won’t work either because once the dart is fired then balloons.numChildren is not equal to zero yet but the darts are. This fires the lost game event but then that last dart can hit that last balloon allowing for the game win event to happen. I didn’t want to have to do this again but here is all of the code for the mini game.

[lua]module(…, package.seeall)

–====================================================================–
– SCENE: TEMPLATE
–====================================================================–

–[[

  • Version: 0.1
  • Made by Ninja Carnival Team @ 2011

******************

  • INFORMATION
    ******************

  • Template

–]]

new = function ( params )

– Imports
local ui = require ( “ui” )

– Groups
local localGroup = display.newGroup()
local bg = display.newGroup()
local mg = display.newGroup()
local fg = display.newGroup()
local balloons = display.newGroup()

– start physics

local physics = require(“physics”)
physics.start()
physics.setGravity(0,9.18 )

–initialize variables

local currentLevel = 1
dartsLeft = 3
local gameState = “start”
local toRemove = {}
local toRemove2 = {}
local statTextSize = 25
local rotDirection = 1
local BALLOON_W = 41
local BALLOON_H = 21
local OFFSET = 23
local W_LEN = 12
local levelText
local levelTextNum
local levelCounter = 1
local levelText = display.newText (“Lvl:”, 0, 0, native.systemFontBold, statTextSize)
local levelTextNum = display.newText(1, 0, 0, native.systemFontBold, statTextSize)
levelTextNum.x = levelTextNum.x + 50

–setup levels
local levels = {}
levels[1] = {{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,3,0,1,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},}

levels[2] = {{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,3,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,5,0,0,0,0,0,0,0,0,2,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},}

levels[3] = {{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,3,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,3,0,0,0,0,0,0,0,3,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,2,0,0,0,0,0,0,0,1,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},}

levels[4] = {{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,3,0,0,0,0,0,0},
{0,2,0,0,0,0,0,4,0,0,0,0},
{0,0,0,0,0,5,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,4,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,5,0},}

levels[5] = {{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,3,0,0,0,3,0,0,0,0},
{0,0,0,0,4,0,4,0,0,0,0,0},
{0,0,0,0,0,5,0,0,0,0,0,0},
{0,0,0,0,4,0,4,0,0,0,0,0},
{0,0,0,3,0,0,0,3,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},}

–Pre-load sounds and music
music = audio.loadStream(“LevelOneSong.mp3”)
sounds = {
pop = audio.loadSound(“balloonPopFinal.mp3”),
blow = audio.loadSound(“BlowingIntoDartGunFinal.mp3”)
}

– Display Objects

local background = display.newImage(“balloonBackground.png”)
local gameExitBtn = display.newImage(“game_exit_final.png”)

– BUTTONS

– Functions


-------creating collision detection--------------------------------------------

local function onCollision(self, event)

if self.name == “dart” and event.other.name == “balloon” then -----determine if the collision between dart and balloon happens

audio.play(sounds.pop) -----play sound effect
table.insert(toRemove, event.other) ----put balloon in table to be removed later

end

end


-------creating the scene------------------------------------------------------

local function createPlayer()

gun = display.newImage(“goldenGunShaded.png”)
gun.name = “gun”
gun.x = display.contentWidth / 2
gun.y = display.contentHeight - 30
gun:setReferencePoint(display.CenterReferencePoint)
gun.xScale = 0.40
gun.yScale = 0.40
mg:insert(gun)
return gun

end

local function gunRotation(event)

gun.rotation = gun.rotation + rotDirection
if gun.rotation > 90 then
rotDirection = -1
elseif gun.rotation < -80 then
rotDirection = 1
end

end

local function fireDart(event)

if dartsLeft ~= 0 then

dartsLeft = dartsLeft - 1
local dart = display.newImage(“dart.png”)
dart.name = “dart”
dart:setReferencePoint(display.CenterReferencePoint)
dart.x = gun.x
dart.y = gun.y
dart.rotation = gun.rotation
physics.addBody(dart, “dynamic”, {density=1.0, friction=0.5, bounce=0.0, radius= 10})
dart.linearDamping = 0.3
dart.angularDamping = 0.8
dart.isBullet = true
dart.isSensor = false
local speed = 325
local velocity = {}
velocity.x = math.cos( math.rad( dart.rotation - 90 ) ) * speed
velocity.y = math.sin( math.rad( dart.rotation - 90 ) ) * speed
audio.play(sounds.blow)
dart:applyForce(velocity.x, velocity.y, dart.x, dart.y)
localGroup:insert(dart)
dart.collision = onCollision
dart:addEventListener(“collision”, dart)

if event.phase == “ended” then

sounds.blow = nil
speed = nil
velocity = nil
dart = nil

end

if dart.isVisible == false then
dart:removeEventListener(“collision”, dart)
localGroup:remove(dart)
dart = nil

end
end
return true
end


----------------draw level-----------------------------------------------------

local function spawnScreenDarts(dartsLeft)

dart2 = display.newImage(“dart.png”)
dart2.name = “dart2”
dart2:setReferencePoint(display.CenterReferencePoint)
dart2.x = 5
dart2.y = display.contentHeight - 25
fg:insert(dart2)

dart3 = display.newImage(“dart.png”)
dart3.name = “dart3”
dart3:setReferencePoint(display.CenterReferencePoint)
dart3.x = 15
dart3.y = display.contentHeight - 25
fg:insert(dart3)

dart4 = display.newImage(“dart.png”)
dart4.name = “dart4”
dart4:setReferencePoint(display.CenterReferencePoint)
dart4.x = 25
dart4.y = display.contentHeight - 25
fg:insert(dart4)
dartsLeft = 3
return dartsLeft
end

function buildLevel(level)
if gameState ~= “game over” then
local len = table.maxn(level)
balloons:toFront()

for i = 1, len do
for j = 1, W_LEN do
if(level[i][j] == 1) then
local balloon = display.newImage(“Blue_Base_Shadows.png”)
balloon.name = “balloon”
balloon.x = BALLOON_W * j - OFFSET
balloon.y = BALLOON_H * i
physics.addBody(balloon, {density = 0.0, friction = 0.0, bounce = 0.05, radius = 15})
balloon.bodyType = ‘static’
balloons.insert(balloons, balloon)
elseif(level[i][j] == 2) then
local balloon = display.newImage(“Yellow_Base_Shadow.png”)
balloon.name = “balloon”
balloon.x = BALLOON_W * j - OFFSET
balloon.y = BALLOON_H * i
physics.addBody(balloon, {density = .5, friction = 0.0, bounce = 0.25, radius = 15})
balloon.bodyType = ‘static’
balloons.insert(balloons, balloon)
elseif(level[i][j] == 3) then
local balloon = display.newImage(“Red_Base_Shadows.png”)
balloon.name = “balloon”
balloon.x = BALLOON_W * j - OFFSET
balloon.y = BALLOON_H * i
physics.addBody(balloon, {density = .5, friction = 0.0, bounce = 0.25, radius = 15})
balloon.bodyType = ‘static’
balloons.insert(balloons, balloon)
elseif(level[i][j] == 4) then
local balloon = display.newImage(“Pink_Base_Shadow.png”)
balloon.name = “balloon”
balloon.x = BALLOON_W * j - OFFSET
balloon.y = BALLOON_H * i
physics.addBody(balloon, {density = .5, friction = 0.0, bounce = 0.25, radius = 15})
balloon.bodyType = ‘static’
balloons.insert(balloons, balloon)
elseif(level[i][j] == 5) then
local balloon = display.newImage(“Green_Base_Shadow.png”)
balloon.name = “balloon”
balloon.x = BALLOON_W * j - OFFSET
balloon.y = BALLOON_H * i
physics.addBody(balloon, {density = .5, friction = 0.0, bounce = 0.25, radius = 15})
balloon.bodyType = ‘static’
balloons.insert(balloons, balloon)
end
end
end
end

end


---------------change levels---------------------------------------------------

local createLevel = function(event)

currentLevel = currentLevel + 1
changeLevel2(levels[currentLevel])

end

function changeLevel2(level)

buildLevel(level)
dartsLeft = 3
dart2.isVisible = true
dart3.isVisible = true
dart4.isVisible = true
Runtime:addEventListener( “enterFrame”, changeLevel)
gameExitBtn:addEventListener(“touch”, removeListeners)
levelTextNum.text = currentLevel

end

function changeLevel(event)

if balloons.numChildren == 0 and gameState == “removing balloons” and table.maxn(levels) ~= currentLevel and gameState ~= “lost” then

timer.performWithDelay(2500, createLevel, 1)
Runtime:removeEventListener( “enterFrame”, changeLevel)
gameExitBtn:removeEventListener(“touch”, removeListeners)

end

end

--------------------------won--------------------------------

function won(event)

if currentLevel == 5 and balloons.numChildren == 0 then

print(“maxed out”)

end

end

--------------------------lost-------------------------------
local popClosed = function ()

–title.removeSelf()

end

local btPopUpt = function ( event )

if gameState == “lost2” then

director:openPopUp( “gameQuit”, popClosed )
end
end

local gameLosing = function (event)

gameState = “lost2”
btPopUpt()

end

function lost(event)

if balloons.numChildren ~= nil then

if gameState == “over” then

if dartsLeft == 0 and balloons.numChildren > 0 then

Runtime:removeEventListener(“enterFrame”, lost)
gameLosing()

end

end

end

end


--------game ending functions--------------------------------------------------

local function endGame()

director:changeScene(“mainmenu”)

end

local function removeOverScreenObjects()

timer.performWithDelay(1000, endGame, 1)

end

local function removeObjects()

localGroup:remove(gun)
localGroup:remove(background)
localGroup:remove(gameExitBtn)
localGroup:remove(balloon)
localGroup:remove(dart2)
localGroup:remove(dart3)
localGroup:remove(dart4)
localGroup:insert(levelText)
localGroup:insert(levelTextNum)
sounds.pop = nil
levelText = nil
levelTextNum = nil
dart2 = nil
dart3 = nil
dart4 = nil
balloon = nil
gun = nil
background = nil
gameExitBtn = nil
rotDirection = nil
BALLOON_W = nil
BALLOON_H = nil
OFFSET = nil
W_LEN = nil
levels = nil
dartsLeft = nil
music = nil
sounds = nil
gameState = nil
timer.performWithDelay(1000, removeOverScreenObjects, 1)

end

function removeListeners(event)

if event.phase == “ended” then
gameState = “game over”
if balloon ~= nil then

for i = 1, #balloon do
display.remove(balloon[i])
balloon[i] = nil
end

end

for i = balloons.numChildren, 1, -1 do

balloons:remove(i)
balloons[i] = nil

end

gameExitBtn:removeEventListener(“touch”, removeListeners)
gun:removeEventListener(“tap”, fireDart)
Runtime:removeEventListener( “enterFrame”, gunRotation)
Runtime:removeEventListener(“enterFrame”, update)
timer.performWithDelay(500, removeObjects(), 1)
end

return true
end


---------update function------------------------------------------------------

local update = function ( event )

for i = #toRemove, 1, -1 do

toRemove[i].parent:remove(toRemove[i])
toRemove[i] = nil

end

for i = #toRemove2,1,-1 do

remove(toRemove2[i])
toRemove2[i] = nil

end

if dartsLeft == 2 then

dart4.isVisible = false
gameState = “removing balloons”

elseif dartsLeft == 1 then

dart3.isVisible = false
gameState = “removing balloons”

elseif dartsLeft == 0 then

dart2.isVisible = false
gameState = “removing balloons”

end

if balloons.numChildren == 0 and gameState == “removing balloons” then

if dart4.isVisible == true then
dart4.isVisible = false
end
if dart3.isVisible == true then
dart3.isVisible = false
end
if dart2.isVisible == true then
dart2.isVisible = false
end
dartsLeft = 0

end

if dartsLeft == 0 and balloons.numChildren ~= 0 then

gameState = “over”

end

end

– PARAMETERS

– Listener
– INITIALIZE
local initVars = function ()
– Inserts
bg:insert(background)
fg:insert(gameExitBtn)
localGroup:insert(bg)
localGroup:insert(mg)
localGroup:insert(fg)
localGroup:insert(balloons)
localGroup:insert(levelText)
localGroup:insert(levelTextNum)

– Positions

gameExitBtn:setReferencePoint(display.CenterReferencePoint)
gameExitBtn.xScale = 0.5
gameExitBtn.yScale = 0.5
gameExitBtn.x = display.contentWidth - 20
gameExitBtn.y = display.contentHeight - 15

– Colors

– Listeners
createPlayer()
buildLevel(levels[1])
spawnScreenDarts()
Runtime:addEventListener( “enterFrame”, gunRotation)
Runtime:addEventListener( “enterFrame”, changeLevel)
gun:addEventListener(“tap”, fireDart)
Runtime:addEventListener(“enterFrame”, update)
gameExitBtn:addEventListener(“touch”, removeListeners)
Runtime:addEventListener(“enterFrame”, won)
Runtime:addEventListener(“enterFrame”, lost)
end

– Initiate variables
initVars()

– MUST return a display.newGroup()
return localGroup

end
[import]uid: 49863 topic_id: 30306 reply_id: 121729[/import]

Ok there are some different ways for you to achieve what you are trying to do. The first one came to mind is doning this. Try to replace the code from line 559 - 563 with this. Im pretty sure this works. And change your if in the won(event) to if gameState == “won” then

[lua]local lastDartThrown = false
local function checkGameState()
if dartsLeft == 0 and balloons.numChildren ~= 0 then
gameState = “over”
else
gameState = “won” --Your win gamestate.
end
end

if dartsLeft == 0 and balloons.numChildren ~= 0 then
if (lastDartThrown == false) then – This to stop multiple timers from starting
lastDartThrown = true
timer.performWithDelay(2500, checkGameState) – After 2,5 sec or the timeframe from dart is thrown until the balloon should have been hit.
end
end[/lua]

Another way, a bit more to change but probably better, is to remove the runtime enterframe listeners for won and lose and instead count down number of darts inside the onCollision(event) function. and if dartsLeft == 0 then checkGameState() end. And then use the above checkGameState function i wrote, but instead of setting the gamestate there you call the won() or lose() functions depending. This because you already dispose the balloons in there, why not count down the darts there aswell. Then when dartsLeft == 0 you start the timer and call checkGameState. [import]uid: 126729 topic_id: 30306 reply_id: 121745[/import]

This is only an ide, but dont dispatch an event when the ball gets thrown, instead check how many balls are left and how many pins are left, maybe with a timer. If ball == 0 and pins > 0 then dispatch the lose event otherwise the won event. Since you havent posted any code its hard to help to pinpoint exactly what you can change. [import]uid: 126729 topic_id: 30306 reply_id: 121406[/import]

I would have post a lot of code for this. I tried the if ball == 0 and pins > 0 the game ends while ball is still being thrown. So in the time it takes the last ball to hit the last pin the lose event starts and once the last pin is hit the next level loads. [import]uid: 49863 topic_id: 30306 reply_id: 121410[/import]

Thats why you should delay the event. You know when the last ball is being thrown, then start a timer for a second or how long it now takes, and then call a function who checkes ball vs pins.
[import]uid: 126729 topic_id: 30306 reply_id: 121412[/import]

Not a bad idea I will try it out. Thanks for the suggestion. [import]uid: 49863 topic_id: 30306 reply_id: 121413[/import]

Well I wasn’t able to figure out a way to make this work. Here is the code maybe it will trigger a light bulb for someone who already knows how to do this. I just picked out the parts I thought were necessary. If something seems to be missing let me know and I will include that as well. Thanks

[lua]-------------in the update function-------

if dartsLeft == 2 then

dart4.isVisible = false
gameState = “removing balloons”

elseif dartsLeft == 1 then

dart3.isVisible = false
gameState = “removing balloons”

elseif dartsLeft == 0 then

dart2.isVisible = false
gameState = “removing balloons”

end

if balloons.numChildren == 0 and gameState == “removing balloons” then

if dart4.isVisible == true then
dart4.isVisible = false
end
if dart3.isVisible == true then
dart3.isVisible = false
end
if dart2.isVisible == true then
dart2.isVisible = false
end
dartsLeft = 0

end

if dartsLeft == 0 and balloons.numChildren ~= 0 then

gameState = “over”

end

-------------------------------the following happens when the player beats a level
– we start building the next level

function changeLevel(event)

if balloons.numChildren == 0 and gameState == “removing balloons” and table.maxn(levels) ~= currentLevel and gameState ~= “lost” then

timer.performWithDelay(2500, createLevel, 1)
Runtime:removeEventListener( “enterFrame”, changeLevel)
gameExitBtn:removeEventListener(“touch”, removeListeners)

end

end

------------------------------------function for losing
function lost(event)

if balloons.numChildren ~= nil then

if gameState == “over” then

if dartsLeft == 0 and balloons.numChildren > 0 then

Runtime:removeEventListener(“enterFrame”, lost)
gameLosing()

end

end

end

end

local gameLosing = function (event)

gameState = “lost2”
btPopUpt()

end

[import]uid: 49863 topic_id: 30306 reply_id: 121533[/import]

I guess you are trying to make the update in the enterFrame listener. If i would try to do this i would do it through a touch listener. I guess you have a touch listener already for making the throws.

Then try something like this:
[lua]local function checkGameState()
if (dartsLeft == 0 and ballonsLeft == 0 ) then
–Call Win
else
–Call Lose
end
end

local function throwDart()
dartsLeft = dartsLeft -1
–Rest of the darts visible checks
if (dartsLeft == 0) then
timer.PerformWithDelay(1000, checkGameState)
end
end

background:addEventListener(“touch”, throwDart) – add the listener to the object you are throwing/touching[/lua]

[import]uid: 126729 topic_id: 30306 reply_id: 121537[/import]

OK that won’t work either because once the dart is fired then balloons.numChildren is not equal to zero yet but the darts are. This fires the lost game event but then that last dart can hit that last balloon allowing for the game win event to happen. I didn’t want to have to do this again but here is all of the code for the mini game.

[lua]module(…, package.seeall)

–====================================================================–
– SCENE: TEMPLATE
–====================================================================–

–[[

  • Version: 0.1
  • Made by Ninja Carnival Team @ 2011

******************

  • INFORMATION
    ******************

  • Template

–]]

new = function ( params )

– Imports
local ui = require ( “ui” )

– Groups
local localGroup = display.newGroup()
local bg = display.newGroup()
local mg = display.newGroup()
local fg = display.newGroup()
local balloons = display.newGroup()

– start physics

local physics = require(“physics”)
physics.start()
physics.setGravity(0,9.18 )

–initialize variables

local currentLevel = 1
dartsLeft = 3
local gameState = “start”
local toRemove = {}
local toRemove2 = {}
local statTextSize = 25
local rotDirection = 1
local BALLOON_W = 41
local BALLOON_H = 21
local OFFSET = 23
local W_LEN = 12
local levelText
local levelTextNum
local levelCounter = 1
local levelText = display.newText (“Lvl:”, 0, 0, native.systemFontBold, statTextSize)
local levelTextNum = display.newText(1, 0, 0, native.systemFontBold, statTextSize)
levelTextNum.x = levelTextNum.x + 50

–setup levels
local levels = {}
levels[1] = {{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,3,0,1,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},}

levels[2] = {{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,3,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,5,0,0,0,0,0,0,0,0,2,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},}

levels[3] = {{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,3,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,3,0,0,0,0,0,0,0,3,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,2,0,0,0,0,0,0,0,1,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},}

levels[4] = {{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,3,0,0,0,0,0,0},
{0,2,0,0,0,0,0,4,0,0,0,0},
{0,0,0,0,0,5,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,4,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,5,0},}

levels[5] = {{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,3,0,0,0,3,0,0,0,0},
{0,0,0,0,4,0,4,0,0,0,0,0},
{0,0,0,0,0,5,0,0,0,0,0,0},
{0,0,0,0,4,0,4,0,0,0,0,0},
{0,0,0,3,0,0,0,3,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},}

–Pre-load sounds and music
music = audio.loadStream(“LevelOneSong.mp3”)
sounds = {
pop = audio.loadSound(“balloonPopFinal.mp3”),
blow = audio.loadSound(“BlowingIntoDartGunFinal.mp3”)
}

– Display Objects

local background = display.newImage(“balloonBackground.png”)
local gameExitBtn = display.newImage(“game_exit_final.png”)

– BUTTONS

– Functions


-------creating collision detection--------------------------------------------

local function onCollision(self, event)

if self.name == “dart” and event.other.name == “balloon” then -----determine if the collision between dart and balloon happens

audio.play(sounds.pop) -----play sound effect
table.insert(toRemove, event.other) ----put balloon in table to be removed later

end

end


-------creating the scene------------------------------------------------------

local function createPlayer()

gun = display.newImage(“goldenGunShaded.png”)
gun.name = “gun”
gun.x = display.contentWidth / 2
gun.y = display.contentHeight - 30
gun:setReferencePoint(display.CenterReferencePoint)
gun.xScale = 0.40
gun.yScale = 0.40
mg:insert(gun)
return gun

end

local function gunRotation(event)

gun.rotation = gun.rotation + rotDirection
if gun.rotation > 90 then
rotDirection = -1
elseif gun.rotation < -80 then
rotDirection = 1
end

end

local function fireDart(event)

if dartsLeft ~= 0 then

dartsLeft = dartsLeft - 1
local dart = display.newImage(“dart.png”)
dart.name = “dart”
dart:setReferencePoint(display.CenterReferencePoint)
dart.x = gun.x
dart.y = gun.y
dart.rotation = gun.rotation
physics.addBody(dart, “dynamic”, {density=1.0, friction=0.5, bounce=0.0, radius= 10})
dart.linearDamping = 0.3
dart.angularDamping = 0.8
dart.isBullet = true
dart.isSensor = false
local speed = 325
local velocity = {}
velocity.x = math.cos( math.rad( dart.rotation - 90 ) ) * speed
velocity.y = math.sin( math.rad( dart.rotation - 90 ) ) * speed
audio.play(sounds.blow)
dart:applyForce(velocity.x, velocity.y, dart.x, dart.y)
localGroup:insert(dart)
dart.collision = onCollision
dart:addEventListener(“collision”, dart)

if event.phase == “ended” then

sounds.blow = nil
speed = nil
velocity = nil
dart = nil

end

if dart.isVisible == false then
dart:removeEventListener(“collision”, dart)
localGroup:remove(dart)
dart = nil

end
end
return true
end


----------------draw level-----------------------------------------------------

local function spawnScreenDarts(dartsLeft)

dart2 = display.newImage(“dart.png”)
dart2.name = “dart2”
dart2:setReferencePoint(display.CenterReferencePoint)
dart2.x = 5
dart2.y = display.contentHeight - 25
fg:insert(dart2)

dart3 = display.newImage(“dart.png”)
dart3.name = “dart3”
dart3:setReferencePoint(display.CenterReferencePoint)
dart3.x = 15
dart3.y = display.contentHeight - 25
fg:insert(dart3)

dart4 = display.newImage(“dart.png”)
dart4.name = “dart4”
dart4:setReferencePoint(display.CenterReferencePoint)
dart4.x = 25
dart4.y = display.contentHeight - 25
fg:insert(dart4)
dartsLeft = 3
return dartsLeft
end

function buildLevel(level)
if gameState ~= “game over” then
local len = table.maxn(level)
balloons:toFront()

for i = 1, len do
for j = 1, W_LEN do
if(level[i][j] == 1) then
local balloon = display.newImage(“Blue_Base_Shadows.png”)
balloon.name = “balloon”
balloon.x = BALLOON_W * j - OFFSET
balloon.y = BALLOON_H * i
physics.addBody(balloon, {density = 0.0, friction = 0.0, bounce = 0.05, radius = 15})
balloon.bodyType = ‘static’
balloons.insert(balloons, balloon)
elseif(level[i][j] == 2) then
local balloon = display.newImage(“Yellow_Base_Shadow.png”)
balloon.name = “balloon”
balloon.x = BALLOON_W * j - OFFSET
balloon.y = BALLOON_H * i
physics.addBody(balloon, {density = .5, friction = 0.0, bounce = 0.25, radius = 15})
balloon.bodyType = ‘static’
balloons.insert(balloons, balloon)
elseif(level[i][j] == 3) then
local balloon = display.newImage(“Red_Base_Shadows.png”)
balloon.name = “balloon”
balloon.x = BALLOON_W * j - OFFSET
balloon.y = BALLOON_H * i
physics.addBody(balloon, {density = .5, friction = 0.0, bounce = 0.25, radius = 15})
balloon.bodyType = ‘static’
balloons.insert(balloons, balloon)
elseif(level[i][j] == 4) then
local balloon = display.newImage(“Pink_Base_Shadow.png”)
balloon.name = “balloon”
balloon.x = BALLOON_W * j - OFFSET
balloon.y = BALLOON_H * i
physics.addBody(balloon, {density = .5, friction = 0.0, bounce = 0.25, radius = 15})
balloon.bodyType = ‘static’
balloons.insert(balloons, balloon)
elseif(level[i][j] == 5) then
local balloon = display.newImage(“Green_Base_Shadow.png”)
balloon.name = “balloon”
balloon.x = BALLOON_W * j - OFFSET
balloon.y = BALLOON_H * i
physics.addBody(balloon, {density = .5, friction = 0.0, bounce = 0.25, radius = 15})
balloon.bodyType = ‘static’
balloons.insert(balloons, balloon)
end
end
end
end

end


---------------change levels---------------------------------------------------

local createLevel = function(event)

currentLevel = currentLevel + 1
changeLevel2(levels[currentLevel])

end

function changeLevel2(level)

buildLevel(level)
dartsLeft = 3
dart2.isVisible = true
dart3.isVisible = true
dart4.isVisible = true
Runtime:addEventListener( “enterFrame”, changeLevel)
gameExitBtn:addEventListener(“touch”, removeListeners)
levelTextNum.text = currentLevel

end

function changeLevel(event)

if balloons.numChildren == 0 and gameState == “removing balloons” and table.maxn(levels) ~= currentLevel and gameState ~= “lost” then

timer.performWithDelay(2500, createLevel, 1)
Runtime:removeEventListener( “enterFrame”, changeLevel)
gameExitBtn:removeEventListener(“touch”, removeListeners)

end

end

--------------------------won--------------------------------

function won(event)

if currentLevel == 5 and balloons.numChildren == 0 then

print(“maxed out”)

end

end

--------------------------lost-------------------------------
local popClosed = function ()

–title.removeSelf()

end

local btPopUpt = function ( event )

if gameState == “lost2” then

director:openPopUp( “gameQuit”, popClosed )
end
end

local gameLosing = function (event)

gameState = “lost2”
btPopUpt()

end

function lost(event)

if balloons.numChildren ~= nil then

if gameState == “over” then

if dartsLeft == 0 and balloons.numChildren > 0 then

Runtime:removeEventListener(“enterFrame”, lost)
gameLosing()

end

end

end

end


--------game ending functions--------------------------------------------------

local function endGame()

director:changeScene(“mainmenu”)

end

local function removeOverScreenObjects()

timer.performWithDelay(1000, endGame, 1)

end

local function removeObjects()

localGroup:remove(gun)
localGroup:remove(background)
localGroup:remove(gameExitBtn)
localGroup:remove(balloon)
localGroup:remove(dart2)
localGroup:remove(dart3)
localGroup:remove(dart4)
localGroup:insert(levelText)
localGroup:insert(levelTextNum)
sounds.pop = nil
levelText = nil
levelTextNum = nil
dart2 = nil
dart3 = nil
dart4 = nil
balloon = nil
gun = nil
background = nil
gameExitBtn = nil
rotDirection = nil
BALLOON_W = nil
BALLOON_H = nil
OFFSET = nil
W_LEN = nil
levels = nil
dartsLeft = nil
music = nil
sounds = nil
gameState = nil
timer.performWithDelay(1000, removeOverScreenObjects, 1)

end

function removeListeners(event)

if event.phase == “ended” then
gameState = “game over”
if balloon ~= nil then

for i = 1, #balloon do
display.remove(balloon[i])
balloon[i] = nil
end

end

for i = balloons.numChildren, 1, -1 do

balloons:remove(i)
balloons[i] = nil

end

gameExitBtn:removeEventListener(“touch”, removeListeners)
gun:removeEventListener(“tap”, fireDart)
Runtime:removeEventListener( “enterFrame”, gunRotation)
Runtime:removeEventListener(“enterFrame”, update)
timer.performWithDelay(500, removeObjects(), 1)
end

return true
end


---------update function------------------------------------------------------

local update = function ( event )

for i = #toRemove, 1, -1 do

toRemove[i].parent:remove(toRemove[i])
toRemove[i] = nil

end

for i = #toRemove2,1,-1 do

remove(toRemove2[i])
toRemove2[i] = nil

end

if dartsLeft == 2 then

dart4.isVisible = false
gameState = “removing balloons”

elseif dartsLeft == 1 then

dart3.isVisible = false
gameState = “removing balloons”

elseif dartsLeft == 0 then

dart2.isVisible = false
gameState = “removing balloons”

end

if balloons.numChildren == 0 and gameState == “removing balloons” then

if dart4.isVisible == true then
dart4.isVisible = false
end
if dart3.isVisible == true then
dart3.isVisible = false
end
if dart2.isVisible == true then
dart2.isVisible = false
end
dartsLeft = 0

end

if dartsLeft == 0 and balloons.numChildren ~= 0 then

gameState = “over”

end

end

– PARAMETERS

– Listener
– INITIALIZE
local initVars = function ()
– Inserts
bg:insert(background)
fg:insert(gameExitBtn)
localGroup:insert(bg)
localGroup:insert(mg)
localGroup:insert(fg)
localGroup:insert(balloons)
localGroup:insert(levelText)
localGroup:insert(levelTextNum)

– Positions

gameExitBtn:setReferencePoint(display.CenterReferencePoint)
gameExitBtn.xScale = 0.5
gameExitBtn.yScale = 0.5
gameExitBtn.x = display.contentWidth - 20
gameExitBtn.y = display.contentHeight - 15

– Colors

– Listeners
createPlayer()
buildLevel(levels[1])
spawnScreenDarts()
Runtime:addEventListener( “enterFrame”, gunRotation)
Runtime:addEventListener( “enterFrame”, changeLevel)
gun:addEventListener(“tap”, fireDart)
Runtime:addEventListener(“enterFrame”, update)
gameExitBtn:addEventListener(“touch”, removeListeners)
Runtime:addEventListener(“enterFrame”, won)
Runtime:addEventListener(“enterFrame”, lost)
end

– Initiate variables
initVars()

– MUST return a display.newGroup()
return localGroup

end
[import]uid: 49863 topic_id: 30306 reply_id: 121729[/import]

Ok there are some different ways for you to achieve what you are trying to do. The first one came to mind is doning this. Try to replace the code from line 559 - 563 with this. Im pretty sure this works. And change your if in the won(event) to if gameState == “won” then

[lua]local lastDartThrown = false
local function checkGameState()
if dartsLeft == 0 and balloons.numChildren ~= 0 then
gameState = “over”
else
gameState = “won” --Your win gamestate.
end
end

if dartsLeft == 0 and balloons.numChildren ~= 0 then
if (lastDartThrown == false) then – This to stop multiple timers from starting
lastDartThrown = true
timer.performWithDelay(2500, checkGameState) – After 2,5 sec or the timeframe from dart is thrown until the balloon should have been hit.
end
end[/lua]

Another way, a bit more to change but probably better, is to remove the runtime enterframe listeners for won and lose and instead count down number of darts inside the onCollision(event) function. and if dartsLeft == 0 then checkGameState() end. And then use the above checkGameState function i wrote, but instead of setting the gamestate there you call the won() or lose() functions depending. This because you already dispose the balloons in there, why not count down the darts there aswell. Then when dartsLeft == 0 you start the timer and call checkGameState. [import]uid: 126729 topic_id: 30306 reply_id: 121745[/import]

Sorry I am having internet issues right now so I am typing from my phone. I tried your method and it works till the game reaches level three then it wont detect losing at all.
[import]uid: 49863 topic_id: 30306 reply_id: 123572[/import]

Is it my solution that dosent work on level 3 or is it the game in general? You have alot of Runtime EventListeners, are you sure that you dispose of them when you clear a played level? Maybe thats the problem. [import]uid: 126729 topic_id: 30306 reply_id: 123701[/import]

Sorry I am having internet issues right now so I am typing from my phone. I tried your method and it works till the game reaches level three then it wont detect losing at all.
[import]uid: 49863 topic_id: 30306 reply_id: 123572[/import]