RemoveSelf yet again.

Hello all, Working testing and debugging my game. I am working a game that has various mini games in it. In three of my games I use the same method for removing objects at the game over screen. For reason in the game whose code I am about post it seems to be incapable of removing anything. I get bugs until I remove all code that has remove in it. If anyone has any ideas I would greatly appreciate it. Oh and the console gives the error of removeself being a nil value and trying to remove things are nil value. Which they aren’t because they are still on the screen and haven’t been removed yet. I know it’s a lot of code but I didn’t want to leave anything out.

[code]
module(…, package.seeall)

–====================================================================–
– SCENE: BALLOON LEVEL
–====================================================================–

–[[

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

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

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

  • Balloon Level

–]]

new = function ( params )

– Imports
local ui = require ( “ui” )

– Groups
local localGroup = display.newGroup()
– start physics
local physics = require(“physics”)
physics.start()
physics.setGravity(0,0 )
– Variables
local levelTimer = 15
local BALLOON_W = 41
local BALLOON_H = 21
local OFFSET = 23
local W_LEN = 12
local balloons = display.newGroup()
local toRemove = {}
local toRemove2 = {}
gameIsActive = true
currentLevel = 1
local gameEvent = ‘’
local levelText
local levelTextNum
local statTextSize = 16
local score = 0
local scoreNum = display.newText(0, 0, 0, native.systemFontBold, statTextSize)
local scoreText = display.newText(“SCORE:”, 0, 0, native.systemFontBold, statTextSize)
local gameTimer = 20
local levelCounter = 1
local levelText = display.newText (“Lvl:”, 0, 0, native.systemFontBold, statTextSize)
local levelTextNum = display.newText(1, 0, 0, native.systemFontBold, statTextSize)
local gameTimerText = display.newText(“20”, 0, 0, native.systemFontBold, statTextSize)
local statsBackground = display.newRect(0,0,_W,scoreText.height + 2.5)

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

–setup levels 1 through 10
local levels = {}
levels[1] = {{0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,0,0,0,3,4,5,4,3,0,0},
{0,0,0,0,1,0,0,0,0,0,1,0},
{0,2,0,0,0,3,0,1,0,3,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,3,0,0,0,0,0,2,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,4,0,0,0,0,0,1,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,1,2,4,5,4,3,1,0,0,0},
{0,0,0,0,0,0,0,0,0,0,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,2,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,3,4,5,4,2,1,2,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,4,0,1,0,3,0,2,0,5,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,1,0,0,0,0,0,0,0,2,0,0},}

levels[4] = {{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,0,0,0,0,5,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,2,0,0,0,3,0,0,0,0,3,0},
{0,0,2,0,4,0,4,0,2,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,2,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},}

levels[5] = {{0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,0,0,0,0,0,0,0,1,0,0},
{0,0,2,0,0,0,0,0,2,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,2,0,0,0,0,0,2,0,0,0},}

– Groups
local localGroup = display.newGroup()
local bg = display.newGroup()
local mg = display.newGroup()
local fg = display.newGroup()
– Display Objects
local background = display.newImage(“Level_1_BG.png”)
– Player Stats
local statTextSize = 16
local score = 0
local scoreNum = display.newText(0, 0, 0, native.systemFontBold, statTextSize)
local scoreText = display.newText(“SCORE:”, 0, 0, native.systemFontBold, statTextSize)
local gameTimer = 20
local gameTimerText = display.newText(“20”, 0, 0, native.systemFontBold, statTextSize)
local statsBackground = display.newRect(0,0,_W,scoreText.height + 2.5)

– Functions

local function onCollision(self, event)
if self.name == “dart” and event.other.name == “balloon” then
score = score + 100
scoreNum.text = score

audio.play(sounds.pop)
table.insert(toRemove, event.other)
balloons.numChildren = balloons.numChildren - 1
table.insert(toRemove2,self)

end

if event.phase == “began” then
if(balloons.numChildren == 0 and table.maxn(levels) ~= currentLevel) then
gameEvent = “win”
timer.performWithDelay(10, nextLevelText, 1)
end
end

if (balloons.numChildren == 0 and table.maxn(levels) == currentLevel) then
gameEvent = “finished”

end

end

local function spawnPlayer ( event )

gun = display.newImage(“blowgun_50X50__2.png”)
gun.name = “gun”
gun.x = display.contentWidth / 2
gun.y = display.contentHeight - 30
gun:setReferencePoint(display.CenterReferencePoint)
–mg:insert(gun)
return gun
end

–rotate gun
local rotDirection = 1
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

–function for firing the dart
local function fireDart(event)

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 = 500
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)
dart.collision = onCollision
dart:addEventListener(“collision”, dart)

end

–function for creating level
function buildLevel(level)

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_Bln_Down.png”)
balloon.name = “balloon”
balloon.x = BALLOON_W * j - OFFSET
balloon.y = BALLOON_H * i
physics.addBody(balloon, {density = .5, friction = 0, bounce = 0, radius = 15})
balloon.bodyType = ‘static’
balloons.insert(balloons, balloon)
elseif(level[i][j] == 2) then
local balloon = display.newImage(“yellow_down.png”)
balloon.name = “balloon”
balloon.x = BALLOON_W * j - OFFSET
balloon.y = BALLOON_H * i
physics.addBody(balloon, {density = .5, friction = 0, bounce = 0, radius = 15})
balloon.bodyType = ‘static’
balloons.insert(balloons, balloon)
elseif(level[i][j] == 3) then
local balloon = display.newImage(“Red_Bln_Down.png”)
balloon.name = “balloon”
balloon.x = BALLOON_W * j - OFFSET
balloon.y = BALLOON_H * i
physics.addBody(balloon, {density = .5, friction = 0, bounce = 0, radius = 15})
balloon.bodyType = ‘static’
balloons.insert(balloons, balloon)
elseif(level[i][j] == 4) then
local balloon = display.newImage(“Pink_Down.png”)
balloon.name = “balloon”
balloon.x = BALLOON_W * j - OFFSET
balloon.y = BALLOON_H * i
physics.addBody(balloon, {density = .5, friction = 0, bounce = 0, radius = 15})
balloon.bodyType = ‘static’
balloons.insert(balloons, balloon)
elseif(level[i][j] == 5) then
local balloon = display.newImage(“Peach_Down.png”)
balloon.name = “balloon”
balloon.x = BALLOON_W * j - OFFSET
balloon.y = BALLOON_H * i
physics.addBody(balloon, {density = .5, friction = 0, bounce = 0, radius = 15})
balloon.bodyType = ‘static’
balloons.insert(balloons, balloon)
end
end
end
end

local updateTimer = function( event )

gameTimer = gameTimer - 1
if gameTimer <= 0 then gameTimer = 0
end
gameTimerText.text = gameTimer

end

function nextLevelText(event)
print(“next Level”)
restart()
end

– function for changing levels
function restart(e)
print(“restart”)

if(gameEvent == ‘win’ and table.maxn(levels) > currentLevel) then
currentLevel = currentLevel + 1
balloons.numChildren = 0

changeLevel(levels[currentLevel])–next level

elseif(gameEvent == ‘win’ and table.maxn(levels) == currentLevel) then
gameEvent = ‘finished’
balloons.numChildren = 0

elseif gameTimer == 0 and balloons.numChildren ~= 0 then
balloons.numChildren = 0
gameEnd()

elseif(gameEvent == ‘finished’ or gameEvent == ‘lose’) then
currentLevel = 0
balloons.numChildren = 0
end
end

function changeLevel(level)

levelCounter = levelCounter + 1
levelTextNum.text = levelCounter
balloons.numChildren = 0
balloons = display.newGroup()
gameTimer = 20
buildLevel(level)
end

function gameEnd( event )

gameEvent = “lose”
overScreenTimer = timer.performWithDelay(1500, gameOverScreen, 1)

end

function gameOverScreen(event)

overScreen = display.newImage(“game_over.png”)
m = " Touch For Title Screen "
msg = display.newText(m, 0, 0, native.systemFont, 12)
msg:setTextColor(254,203,50)
msg:setReferencePoint(display.CenterReferencePoint)
msg.x = display.contentWidth * 0.5
msg.y = display.contentHeight * 0.75
msg:addEventListener(“touch”, titleScreen)
bg:removeSelf()
mg:removeSelf()
fg:removeSelf()
end

function titleScreen( event)

audio.pause(music)
gameEvent = nil
director:changeScene(“mainmenu”)
overScreen:removeSelf()
msg:removeSelf()

end

– INITIALIZE
local initVars = function ()
– Inserts
bg:insert(background)
spawnPlayer()
fg:insert(statsBackground)
fg:insert(scoreText)
fg:insert(gameTimerText)
fg:insert(scoreNum)
–fg:insert(levelCounter)
fg:insert(levelText)
fg:insert(levelTextNum)

– score positions

scoreText.x = (scoreText.width * 0.5) + 10
scoreText.y = (scoreText.height * 0.5) + 5
scoreNum:setReferencePoint(display.CenterLeftReferencePoint)
scoreNum.x = scoreText.width + 30
scoreNum.y = scoreText.y
levelText.x = (display.contentWidth * 0.75) + 25
levelText.y = levelText.height * 0.5
levelTextNum.x = (display.contentWidth * 0.75) + 45
levelTextNum.y = levelTextNum.height * 0.5

– timer position
gameTimerText.x = _W * 0.5
gameTimerText.y = scoreText.y

– Colors
statsBackground:setFillColor(50,0,0)
scoreText:setTextColor(255,0,0)
scoreNum:setTextColor(255,0,0)
levelText:setTextColor(255,0,0)
levelTextNum:setTextColor(255,0,0)
gameTimerText:setTextColor(255,0,0)

– Listeners
Runtime:addEventListener( “enterFrame”, gunRotation)
gun:addEventListener(“tap”, fireDart)
buildLevel(levels[1])
timer.performWithDelay(1000, updateTimer, 60)

audio.play(music, {loops = -1})

end

– Initiate variables
initVars()

– Update
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
toRemove2[i].parent:remove(toRemove2[i])
toRemove2[i] = nil
end

if balloons.numChildren ~= 0 and gameTimer == 0 then
gameEnd()
end

end

Runtime:addEventListener(“enterFrame”, update)
– MUST return a display.newGroup()
return localGroup

end [import]uid: 49863 topic_id: 23650 reply_id: 323650[/import]

A couple of issues I see right away are around your groups…

This line is declared twice… (not a huge deal but get rid of one)

 local localGroup = display.newGroup()  

From there, remember that since you are using director any other groups you create to manage your scene need to be added to localGroup.

So even though you have bg,mg and fg groups I’m not seeing anywhere that you put these into localGroup. THis means they are running rogue and you will get unexpected results with your layering, removal, etc.

Also each and every display object you create needs to be inserted into one of your groups for proper removal, layering. (EX: overScreen and msg need to find their ways into a displayGroup)

Take some some time and make sure of these things and I think you will see it clean up better and you are probably not seeing things in the correct layer that you are hoping right now.

Just remember to get everything into a display group and if you have more than just localGroup (which is totally a common way to organize) be sure to insert those groups into localGroup.

Hope that helps get you in the right track, just needs some cleanup on aisle #4 LOL

Good luck!
Croisened

[import]uid: 48203 topic_id: 23650 reply_id: 94962[/import]

When I attempt to do this my rectangle for stats turns white and and the score, timer, and level are all gone.

[code]

bg:insert(background)
spawnPlayer()
fg:insert(statsBackground)
fg:insert(scoreText)
fg:insert(gameTimerText)
fg:insert(scoreNum)
fg:insert(levelText)
fg:insert(levelTextNum)
localGroup:insert(bg,mg,fg) [import]uid: 49863 topic_id: 23650 reply_id: 94974[/import]

Plus I still can’t remove anything.
[import]uid: 49863 topic_id: 23650 reply_id: 95004[/import]

OK I figured out why the statsbackground was doing that. It’s the same reason for the double you noticed earlier. Not sure how all of that got in there but I took it out. I took another shot at putting all of my display groups into one localGroup. I still can’t remove anything without getting an error. [import]uid: 49863 topic_id: 23650 reply_id: 95005[/import]

Yeah I am lost. This works on everything else I have ever tried. I have no clue. [import]uid: 49863 topic_id: 23650 reply_id: 95047[/import]

Bump [import]uid: 49863 topic_id: 23650 reply_id: 95238[/import]

Sorry I keep bumping this but I am in a real jam here. [import]uid: 49863 topic_id: 23650 reply_id: 95450[/import]

Helping where I can, I think you have yourself in some cut and paste mess.

You need to clean up ALL your duplicate declarations, look at lines 122-129 - those are all duplicated and is the reason for your messed up stats background.

Also, you are attempting to use _W which has never been defined, at least no in the code provided.
Getting your sub groups into localGroup is definitely a step in the right direction so keep that, if you fix what I mentioned above your display issues will work fine, except when the game ends you have some issues to work through in your game over event handlers in that they don’t stop firing so you will need to tackle that next.

Keep going, add print statements and watch your output and you’ll get there.

–Croisened

[import]uid: 48203 topic_id: 23650 reply_id: 95620[/import]

Thanks man. I am glad you didn’t think I was ignoring you. I am trying to follow you advise you are giving. I am getting that same error message when I try to remove the event handlers. It’s say they are nil. Just like the display objects. [import]uid: 49863 topic_id: 23650 reply_id: 95840[/import]

OK I am taking your advise. I went in and removed the doubles. I then added all subgroups to localGroup. Now when I am trying to remove the objects I am a bit lost. I tried localGroup:remove(bg) and that didn’t work so I figured the subgroups were destroyed when added to localGroup so then I tried localGroup:remove(background) and then background = nil. The console stopped telling me I had a nil error but then it only removes one quarter of the background from the scene. So I am sure there is an issue with the way I removed it. Here is my code so far. Oh and don’t worry about the _W. It was defined in a different file.

[code]
module(…, package.seeall)

–====================================================================–
– SCENE: BALLOON LEVEL
–====================================================================–

–[[

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

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

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

  • Balloon Level

–]]

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 lg = display.newGroup()
– start physics
local physics = require(“physics”)
physics.start()
physics.setGravity(0,0 )
– Variables
local levelTimer = 15
local BALLOON_W = 41
local BALLOON_H = 21
local OFFSET = 23
local W_LEN = 12
local balloons = display.newGroup()
local toRemove = {}
local toRemove2 = {}
gameIsActive = true
currentLevel = 1
local gameEvent = ‘’
local levelText
local levelTextNum
local statTextSize = 16
local score = 0
local scoreNum = display.newText(0, 0, 0, native.systemFontBold, statTextSize)
local scoreText = display.newText(“SCORE:”, 0, 0, native.systemFontBold, statTextSize)
local gameTimer = 20
local levelCounter = 1
local levelText = display.newText (“Lvl:”, 0, 0, native.systemFontBold, statTextSize)
local levelTextNum = display.newText(1, 0, 0, native.systemFontBold, statTextSize)
local gameTimerText = display.newText(“20”, 0, 0, native.systemFontBold, statTextSize)

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

–setup levels 1 through 10
local levels = {}
levels[1] = {{0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,0,0,0,3,4,5,4,3,0,0},
{0,0,0,0,1,0,0,0,0,0,1,0},
{0,2,0,0,0,3,0,1,0,3,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,3,0,0,0,0,0,2,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,4,0,0,0,0,0,1,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,1,2,4,5,4,3,1,0,0,0},
{0,0,0,0,0,0,0,0,0,0,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,2,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,3,4,5,4,2,1,2,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,4,0,1,0,3,0,2,0,5,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,1,0,0,0,0,0,0,0,2,0,0},}

levels[4] = {{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,0,0,0,0,5,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,2,0,0,0,3,0,0,0,0,3,0},
{0,0,2,0,4,0,4,0,2,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,2,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},}

levels[5] = {{0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,0,0,0,0,0,0,0,1,0,0},
{0,0,2,0,0,0,0,0,2,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,2,0,0,0,0,0,2,0,0,0},}

– Display Objects
local background = display.newImage(“Level_1_BG.png”)
– Player Stats
local statsBackground = display.newRect(0,0,_W,scoreText.height + 2.5)

– Functions

local function onCollision(self, event)
if self.name == “dart” and event.other.name == “balloon” then
score = score + 100
scoreNum.text = score

audio.play(sounds.pop)
table.insert(toRemove, event.other)
balloons.numChildren = balloons.numChildren - 1
table.insert(toRemove2,self)

end

if event.phase == “began” then
if(balloons.numChildren == 0 and table.maxn(levels) ~= currentLevel) then
gameEvent = “win”
timer.performWithDelay(10, nextLevelText, 1)
end
end

if (balloons.numChildren == 0 and table.maxn(levels) == currentLevel) then
gameEvent = “finished”

end

end

local function spawnPlayer ( event )

gun = display.newImage(“blowgun_50X50__2.png”)
gun.name = “gun”
gun.x = display.contentWidth / 2
gun.y = display.contentHeight - 30
gun:setReferencePoint(display.CenterReferencePoint)
–mg:insert(gun)
return gun
end

–rotate gun
local rotDirection = 1
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

–function for firing the dart
local function fireDart(event)

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 = 500
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)
dart.collision = onCollision
dart:addEventListener(“collision”, dart)

end

–function for creating level
function buildLevel(level)

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_Bln_Down.png”)
balloon.name = “balloon”
balloon.x = BALLOON_W * j - OFFSET
balloon.y = BALLOON_H * i
physics.addBody(balloon, {density = .5, friction = 0, bounce = 0, radius = 15})
balloon.bodyType = ‘static’
balloons.insert(balloons, balloon)
elseif(level[i][j] == 2) then
local balloon = display.newImage(“yellow_down.png”)
balloon.name = “balloon”
balloon.x = BALLOON_W * j - OFFSET
balloon.y = BALLOON_H * i
physics.addBody(balloon, {density = .5, friction = 0, bounce = 0, radius = 15})
balloon.bodyType = ‘static’
balloons.insert(balloons, balloon)
elseif(level[i][j] == 3) then
local balloon = display.newImage(“Red_Bln_Down.png”)
balloon.name = “balloon”
balloon.x = BALLOON_W * j - OFFSET
balloon.y = BALLOON_H * i
physics.addBody(balloon, {density = .5, friction = 0, bounce = 0, radius = 15})
balloon.bodyType = ‘static’
balloons.insert(balloons, balloon)
elseif(level[i][j] == 4) then
local balloon = display.newImage(“Pink_Down.png”)
balloon.name = “balloon”
balloon.x = BALLOON_W * j - OFFSET
balloon.y = BALLOON_H * i
physics.addBody(balloon, {density = .5, friction = 0, bounce = 0, radius = 15})
balloon.bodyType = ‘static’
balloons.insert(balloons, balloon)
elseif(level[i][j] == 5) then
local balloon = display.newImage(“Peach_Down.png”)
balloon.name = “balloon”
balloon.x = BALLOON_W * j - OFFSET
balloon.y = BALLOON_H * i
physics.addBody(balloon, {density = .5, friction = 0, bounce = 0, radius = 15})
balloon.bodyType = ‘static’
balloons.insert(balloons, balloon)
end
end
end
end

local updateTimer = function( event )

gameTimer = gameTimer - 1
if gameTimer <= 0 then gameTimer = 0
end
gameTimerText.text = gameTimer

end

function nextLevelText(event)
print(“next Level”)
restart()
end

– function for changing levels
function restart(e)
print(“restart”)

if(gameEvent == ‘win’ and table.maxn(levels) > currentLevel) then
currentLevel = currentLevel + 1
balloons.numChildren = 0

changeLevel(levels[currentLevel])–next level

elseif(gameEvent == ‘win’ and table.maxn(levels) == currentLevel) then
gameEvent = ‘finished’
balloons.numChildren = 0

elseif gameTimer == 0 and balloons.numChildren ~= 0 then
balloons.numChildren = 0
gameEnd()

elseif(gameEvent == ‘finished’ or gameEvent == ‘lose’) then
currentLevel = 0
balloons.numChildren = 0
end
end

function changeLevel(level)

levelCounter = levelCounter + 1
levelTextNum.text = levelCounter
balloons.numChildren = 0
balloons = display.newGroup()
gameTimer = 20
buildLevel(level)
end

function gameEnd( event )

gameEvent = “lose”
overScreenTimer = timer.performWithDelay(1500, gameOverScreen, 1)

end

function gameOverScreen(event)

–localGroup:remove(bg)
localGroup:remove(background)
background = nil
overScreen = display.newImage(“game_over.png”)
m = " Touch For Title Screen "
msg = display.newText(m, 0, 0, native.systemFont, 12)
msg:setTextColor(254,203,50)
msg:setReferencePoint(display.CenterReferencePoint)
msg.x = display.contentWidth * 0.5
msg.y = display.contentHeight * 0.75
msg:addEventListener(“touch”, titleScreen)
lg:insert(overScreen,msg)
localGroup:insert(lg)
end

function titleScreen( event)

audio.pause(music)
gameEvent = nil
director:changeScene(“mainmenu”)
–overScreen:removeSelf()
–msg:removeSelf()

end

– INITIALIZE
local initVars = function ()
– Inserts
bg:insert(background)
spawnPlayer()
fg:insert(statsBackground)
fg:insert(scoreText)
fg:insert(gameTimerText)
fg:insert(scoreNum)
fg:insert(levelText)
fg:insert(levelTextNum)

localGroup:insert(bg,mg,fg)
print(localGroup.numChildren)
– score positions

scoreText.x = (scoreText.width * 0.5) + 10
scoreText.y = (scoreText.height * 0.5) + 5
scoreNum:setReferencePoint(display.CenterLeftReferencePoint)
scoreNum.x = scoreText.width + 30
scoreNum.y = scoreText.y
levelText.x = (display.contentWidth * 0.75) + 25
levelText.y = levelText.height * 0.5
levelTextNum.x = (display.contentWidth * 0.75) + 45
levelTextNum.y = levelTextNum.height * 0.5

– timer position
gameTimerText.x = _W * 0.5
gameTimerText.y = scoreText.y

– Colors
statsBackground:setFillColor(50,0,0)
scoreText:setTextColor(255,0,0)
scoreNum:setTextColor(255,0,0)
levelText:setTextColor(255,0,0)
levelTextNum:setTextColor(255,0,0)
gameTimerText:setTextColor(255,0,0)

– Listeners
Runtime:addEventListener( “enterFrame”, gunRotation)
gun:addEventListener(“tap”, fireDart)
buildLevel(levels[1])
timer.performWithDelay(1000, updateTimer, 60)

–audio.play(music, {loops = -1})

end

– Initiate variables
initVars()

– Update
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
toRemove2[i].parent:remove(toRemove2[i])
toRemove2[i] = nil
end

if balloons.numChildren ~= 0 and gameTimer == 0 then
gameEnd()
end

end

Runtime:addEventListener(“enterFrame”, update)
– MUST return a display.newGroup()
return localGroup

end [import]uid: 49863 topic_id: 23650 reply_id: 97827[/import]

bump [import]uid: 49863 topic_id: 23650 reply_id: 98381[/import]

Alright I think I got this worked out. Thanks for the help. [import]uid: 49863 topic_id: 23650 reply_id: 100098[/import]