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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,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]