Hello there,
I am new to Corona SDK and I am loving it. I am trying to finish up this game I have been working on. Essentially I have everything working fine, except, I am trying to change levels when the game is over unsuccessfully.
Basically all I need to be able to do is when the conditions for the round/game are met, the changeScene() command needs to go to the next screen (so far that I am able to do) but I NEED to make it so that all of my eventListeners and all of my animations (sprites) are all gone from the screen before going to the next one. Here is the code I have:
--
-- Project: Bombz
-- Description: Birdies that defecate on top of a green man, and the green man will need to dodge the
--poo!
-- 300 x 137
-- Version: 1.0
-- Managed with http://CoronaProjectManager.com
--
-- Copyright 2011 Paulo Dichone. All Rights Reserved.
-- Description: This is the Level\_1 lua where we show the game level 1.
module(..., package.seeall);
--system.setAccelerometerInterval( 100.00 ) -- default: 75.0 (30fps) or 62.0 for 60 fps
function new()
level\_1\_group = display.newGroup()
front\_group = display.newGroup()
system.setIdleTimer( false ) --prevents the phone from dimming while playing the game
local physics = require("physics");
local director = require("director")
local movieclip = require("movieclip");
physics.start();
--physics.setDrawMode("hybrid");
--SETTING GRAVITY --
physics.setGravity(0, 0); --just like on Earth
local director = require("director");
require "sprite";
--VARIABLES WIDTH AND HEIGHT --
local \_W = display.contentWidth;
local \_H = display.contentHeight;
-- DEFINING BOUNDARIES --
local floor\_bottom = display.newRect(\_W,0,17,1);
local leftWall = display.newRect(0,0,0, \_H);
local rightWall = display.newRect(\_W, 0, 1, \_H);
--WRAPPIN WALLS WITH PHYSICS ENGINE --
physics.addBody(leftWall, "static", {bounce = 0.01});
physics.addBody(rightWall, "static", {bounce = 0.01});
physics.addBody(floor\_bottom, "static", {bounce = 1, friction = 0.3;});
--OBJECTS
local clouds1;
local clouds2;
local clouds3;
local clouds4;
--AUDIO SET UP --
local gun\_sound = audio.loadSound("pop.wav");
local soundtrack = audio.loadSound("background\_soundtrack.wav");
local shit\_splat = audio.loadSound("shit\_splat.wav");
local ouch\_sound = audio.loadSound("Grunt.wav");
local crow\_sound = audio.loadStream("crow.wav");
local crow\_soundChannel = audio.play( crow\_sound, { channel = 2, loop = 1, fadein = 4000})
local backgroundMusic = audio.loadStream("background\_soundtrack.wav")
local backgroundMusicChannel = audio.play( backgroundMusic, { channel=1, loops=-1, fadein=5000 } )
-- AUDIO
local tapSound = audio.loadSound( "tapsound.wav" )
local blastOffSound = audio.loadSound( "blastoff.wav" )
local ghostPoofSound = audio.loadSound( "ghostpoof.wav" )
local monsterPoofSound = audio.loadSound( "monsterpoof.wav" )
local impactSound = audio.loadSound( "impact.wav" )
local weeSound = audio.loadSound( "wee.wav" )
local newRoundSound = audio.loadSound( "newround.wav" )
local youWinSound = audio.loadSound( "youwin.wav" )
local youLoseSound = audio.loadSound( "youlose.wav" )
local backgroundMusic = audio.loadStream("footsteps.mp3")
---START PLAYING MUSIC
local function playMusic()
local backgroundMusicChannel = audio.play( backgroundMusic, { channel=1, loops=-1, fadein=5000 } )
end
Runtime:addEventListener("enterFrame", playMusic)
local country\_side\_image = display.newImageRect("country\_side.jpg", 480, 320);
country\_side\_image.x = 240; country\_side\_image.y = 160;
level\_1\_group:insert(country\_side\_image);
-- start the clouds and the bird animation--
local drawClouds = function()
cloud = display.newImageRect( "clouds-right.png", 480, 40 )
cloud:setReferencePoint(display.TopLeftReferencePoint);
cloud.alpha = 0.5;
cloud:scale(4,4);
cloud.x = -400;
cloud.y = -600;
level\_1\_group:insert(cloud)
--DEFINING GROUND --
local ground = display.newImageRect("ground1.png", 480, 50);
ground.x = \_W \* 0.5; ground.y = \_H \* 0.5 + 130;
local groundShape = {-\_W,-2, 7,-10, \_W,7, -4,23 }
physics.addBody(ground , "static", {bounce = 1, friction = 2, density = 2, height = 0, shape = groundShape});
front\_group:insert(ground)
--MOV CLOUDS ---
local baseline = 223
-- CLOUDS
clouds1 = display.newImageRect( "clouds-left.png", 480, 320 )
clouds1.x = 240; clouds1.y = 160
clouds2 = display.newImageRect( "clouds-right.png", 480, 320 )
clouds2.x = 720; clouds2.y = 160
clouds3 = display.newImageRect( "clouds-left.png", 480, 320 )
clouds3.x = 1200; clouds3.y = 160
clouds4 = display.newImageRect( "clouds-right.png", 480, 320 )
clouds4.x = 1680; clouds4.y = 160
level\_1\_group:insert( clouds1 )
level\_1\_group:insert( clouds2 )
level\_1\_group:insert( clouds3 )
level\_1\_group:insert( clouds4 )
local tPrevious = system.getTimer ( );
local function animateClouds( event )
local tDelta = event.time - tPrevious
tPrevious = event.time
local xOffset = ( 0.01 \* tDelta)
clouds1.x = clouds1.x - xOffset
clouds2.x = clouds2.x - xOffset
clouds3.x = clouds3.x - xOffset
clouds4.x = clouds4.x -xOffset
if ( clouds1.x + clouds1.contentWidth) \< 0 then
clouds1: translate ( 480 \* 2, 0 )
end
if ( clouds2.x + clouds2.contentWidth) \< 0 then
clouds2:translate(480 \* 2, 0)
end
if ( clouds3.x + clouds3.contentWidth) \< 0 then
clouds3:translate( 480 \* 2, 0)
end
if ( clouds4.x + clouds4.contentWidth ) \< 0 then
clouds4:translate( 480 \* 2, 0)
end
end
Runtime:addEventListener("enterFrame", animateClouds)
-- TREES
local treesLeft = display.newImageRect( "trees-left.png", 480, 320 )
treesLeft.x = 240; treesLeft.y = 170
local treesRight = display.newImageRect( "trees-right.png", 480, 320 )
treesRight.x = 720; treesRight.y = 170
level\_1\_group:insert( treesLeft )
level\_1\_group:insert( treesRight )
end
------------START COUNTER----------------------
local function startTime( event )
local ellapsedTime = score.setScore( score.getScore() + 1)
end
local score\_timer = timer.performWithDelay(1000, startTime, 0)
---------------------------------------------
-------------------------------------------------
--- TIMER FOR THE GAME CYCLE ---
-------------------------------------------------
--show a basic white bar --
--[[ local timeBar = display.newRect( 20, 165, 280, 20)
timeBar:setReferencePoint(display.BottomLeftReferencePoint)
timeBar.x = 20
timeBar.y = (\_H\* 0.5) - 120]]
-----DISPLAY LIFE ICONS -----------
local lifeIcons = { }
local lives = 7;
local maxLives = 7;
local i
for i = 1, maxLives do
lifeIcons[i] = display.newImageRect("lifeicon.png", 25, 25)
lifeIcons[i].x = 14 + (lifeIcons[i].contentWidth \* (i - 1))
lifeIcons[i].y = 40
-- level\_1\_group:insert(lifeIcons[i])
front\_group:insert(lifeIcons[i])
end
--make bar shrink over time
--[[ local function loseTime( event )
timeBar.width = timeBar.width - 2
timeBar:setReferencePoint(display.BottomLeftReferencePoint)
timeBar.x = 20
if ( timeBar.width == 2) then
timeBar:removeSelf()
print("game Over!")
timer.cancel(gameTimer)
director:changeScene("mainmenu")
--gameover()
end
end
-- Runtime:addEventListener("enterFrame", loseTime)
gameTimer = timer.performWithDelay(100, loseTime, 0)]]
--VULTURE ANIMATION --
local vulture\_sheet = sprite.newSpriteSheet("gloomy\_vulture.png", 117, 100)
--defining the sprite --
local vulture\_set = sprite.newSpriteSet(vulture\_sheet, 1, 2)
sprite.add(vulture\_set, "vulture", 1, 2, 12500, 0)
--give the sprite a name position --
local vulture = sprite.newSprite(vulture\_set)
vulture.x = 420
vulture.y = 67
front\_group:insert(vulture)
--vulture ready to play
vulture:prepare("vulture")
vulture:play("vulture")
--START ANDREW BIRD (MAIN BIRD --
local main\_bird\_sheet = sprite.newSpriteSheet("birdie\_right\_left.png", 77, 56)
local main\_bird\_set = sprite.newSpriteSet(main\_bird\_sheet, 1, 4)
sprite.add(main\_bird\_set, "main\_bird", 1, 4, 256, 0)
local main\_bird = sprite.newSprite(main\_bird\_set)
main\_bird.x = math.random(5)
main\_bird.y = 70
main\_bird:scale(-1, 1)
main\_bird:prepare("main\_bird")
main\_bird:play("main\_bird")
front\_group:insert(main\_bird)
--START BLUE BIRD ACTION --
local blue\_bird\_sheet = sprite.newSpriteSheet("bird\_sprite\_right\_left.png", 60 , 40)
local blue\_bird\_set = sprite.newSpriteSet(blue\_bird\_sheet, 1, 4)
sprite.add(blue\_bird\_set, "blue\_bird", 1, 4, 210, 0)
local blue\_bird = sprite.newSprite(blue\_bird\_set)
blue\_bird.x = 34
blue\_bird.y = 90
blue\_bird:scale(-1, 1)
blue\_bird:prepare("blue\_bird")
blue\_bird:play("blue\_bird")
front\_group:insert(blue\_bird)
--BLACK BIRD ANIMATION --
local black\_bird\_sheet = sprite.newSpriteSheet("birdie\_right\_left.png", 77, 56)
local black\_bird\_set = sprite.newSpriteSet(black\_bird\_sheet, 1, 4)
sprite.add(black\_bird\_set, "black\_bird", 1, 4, 400, 0)
local black\_bird = sprite.newSprite(black\_bird\_set)
black\_bird.x = math.random( 5)
black\_bird.y = 60
black\_bird:scale(-1, 1)
black\_bird:prepare("black\_bird")
black\_bird:play("black\_bird")
front\_group:insert(black\_bird)
---HERO SPRITE (THE MAIN ACTOR)--
local herosheet = sprite.newSpriteSheet("greenman.png", 128, 128)
--defining the sprite we are going to be using
local heroset = sprite.newSpriteSet(herosheet, 1, 15)
sprite.add(heroset, "hero", 1, 15, 200, 0)
--Giving the sprite a name and positioning it
local hero = sprite.newSprite(heroset);
hero.x = 160
hero.y = \_H \* 0.5 + 70;
front\_group:insert(hero)
--sprite is ready to go --
hero:prepare("hero")
hero:play("hero")
-- hero.isFixedRotation = true
physics.addBody(hero, "static", { radius = 20})
--------------------------------------------
---- MOVEMENT ----
--------------------------------------------
local motionx = 0;
local motiony = 0;
local speed = 5
-- On tilt we redefine motionx
local function onAccelerate( event )
motionx = 35 \* event.yGravity
--motiony = 30 \* event.xGravity
if( motionx == -1) then --onTilt change the direction of the sprite
hero:scale(-1, 1)
elseif ( motionx == 1) then
hero:scale(-1, 1)
end
end
local laccel = Runtime:addEventListener("accelerometer", onAccelerate)
--When device is tilted, move hero----
local function movehero ( event )
hero.x = hero.x - motionx
end
Runtime: addEventListener("enterFrame", movehero)
--keep the hero on screen --
local function constrainHero ( event )
if hero.x \< 2 then
hero.x = 20
--hero:scale(-1, 1)
elseif hero.x \> 480 then
hero.x = 480
--hero:scale(-1, 1)
end
end
Runtime:addEventListener("enterFrame", constrainHero)
local speed = 1;
local direction\_x = 1 ;
local direction\_y = 1;
local direction\_birdie = 1;
local direction\_blue\_birdie = 1;
local direction\_green\_man = 1;
local direction\_black\_bird = 1;
local motionx = 0;
local speed\_green\_man = 4;
---ACTION FUNCTION FOR CONTROLING THE BLUE BIRD --
local function blueAction(e)
--audio.play(crow\_sound);
blue\_bird.x = blue\_bird.x + (speed+1) \* direction\_blue\_birdie;
if ( blue\_bird.x \> display.contentWidth) then
direction\_blue\_birdie = direction\_blue\_birdie \* -1; -- go opposite direction
blue\_bird:scale(-1, 1);
elseif ( blue\_bird.x \< 0) then
direction\_blue\_birdie = direction\_blue\_birdie \* -1;
blue\_bird:scale(-1, 1);
end
end
Runtime:addEventListener("enterFrame", blueAction)
-- timer.performWithDelay( 13200, blueAction, 10)
--BLACK BIRD ACTION --
local function blackBirdAction( event )
black\_bird.x = black\_bird.x + (speed + 1.1) \* direction\_black\_bird
if ( black\_bird .x \> display.contentWidth) then
direction\_black\_bird = direction\_black\_bird \* -1;
black\_bird:scale(-1, 1)
elseif ( black\_bird.x \< 0) then
direction\_black\_bird = direction\_black\_bird \* -1
black\_bird:scale(-1, 1)
end
end
Runtime:addEventListener("enterFrame", blackBirdAction)
local function action (e)
main\_bird.x = main\_bird.x + speed \* direction\_birdie;
physics.setGravity(0, 8)
if ( main\_bird.x \> display.contentWidth ) then
--audio.play(crow\_sound);
direction\_birdie = direction\_birdie \* -1;
main\_bird:scale(-1, 1);
elseif ( main\_bird.x \< 0) then
direction\_birdie = direction\_birdie \* -1;
main\_bird:scale(-1,1);
end
end
Runtime:addEventListener("enterFrame",action);
--create bombz for the blue bird --
local function spawnBombzBlue()
local bombz\_blue = display.newImageRect("poo.png", 15, 15)
bombz\_blue.x = blue\_bird.x;
bombz\_blue.y = blue\_bird.y + 18
physics.addBody(bombz\_blue, {bounce = 0.2, density = 0.4, friction = 1, radius = 5})
bombz\_blue.name = "blue\_poo"
-- ON COLLISION WITH THE GROUND --
local function onBombzBlue( self, event)
if ( event.phase == "began" ) then
self:removeSelf()
return true
end
end
bombz\_blue.collision = onBombzBlue
bombz\_blue:addEventListener("collision", bombz\_blue)
end
local spawn\_blue\_timer = timer.performWithDelay(1300, spawnBombzBlue, 50)
--Create the bombz --
local function spawnBombz()
local bombz = display.newImageRect("poo.png",20, 20);
bombz.x = main\_bird.x; bombz.y = main\_bird.y + 22;
physics.addBody(bombz, {bounce = 0.2, density = 0.4, friction = 1, radius = 5});
bombz.name = "bombz"
--ON COLLISION WITH THE GROUND ---
local function onBombzCollision( self, event )
if ( event.phase == "began" ) then
-- audio.play(shit\_splat);
self:removeSelf()
return true
end
end
bombz.collision = onBombzCollision
bombz:addEventListener( "collision", bombz )
--ON COLLISION WITH THE GREENMAN --
local function onGreenManCollision( self, event)
if ( event.other.name == "blue\_poo" or event.other.name == " bombz" or event.other.name == "black\_poo") then
if lives \> 0 then
lifeIcons[lives].alpha = 0.3
lives = lives - 1
end
if lives == 0 then
print("Game Over!")
--gameover()
--director:changeScene( "mainmenu" )
end
end
if ( event.phase == "began") then
audio.play(ouch\_sound);
end
end
hero.collision = onGreenManCollision;
hero:addEventListener("collision", hero);
end
local spawn\_bombz\_timer = timer.performWithDelay (math.random(500, 3700), spawnBombz, 50 );
--CREATE BOMBZ FOR THE SECOND MAIN BIRD --
local function spawnBombzBlack()
local bombz\_black = display.newImageRect("poo.png", 20, 20)
bombz\_black.x = black\_bird.x; bombz\_black.y = black\_bird.y + 23;
physics.addBody(bombz\_black, {bounce = 0.2, density = 0.3, friction = 1, radius = 5})
bombz\_black.name = "black\_poo"
--ON COLLISION WITH THE GROUND --
local function onBombzBlackCollision( self, event)
if ( event.phase == "began") then
self:removeSelf()
return true
end
end
bombz\_black.collision = onBombzBlackCollision
bombz\_black:addEventListener("collision", bombz\_black)
end
bombz\_timer = timer.performWithDelay(1900, spawnBombzBlack, 50)
--------------------------------------------------------------------------
-- SCORE --
--------------------------------------------------------------------------
score = require("score")
-- get information about the score placard
local scoreInfo = score.getInfo()
-- initialize the score placard's location
score.init( {
x = (\_W / 2) + 200,
y = (\_H / 2 ) + 70 }
)
score.setScore(0)
-------------------------------------------------------------
-- MOVING CLOUDS ----
-------------------------------------------------------------
drawClouds(); --DRAWCLOUDS --
-----GAME OVER ---
function gameover (event)
if lives == 0 then
Runtime:removeEventListener("accelerometer", laccel)
Runtime:removeEventListener("enterFrame", animateClouds)
Runtime:removeEventListener("enterFrame", movehero)
Runtime:removeEventListener("enterFrame", constrainHero)
Runtime:removeEventListener("enterFrame", blueAction)
Runtime:removeEventListener("enterFrame", blackBirdAction)
Runtime:removeEventListener("enterFrame", action)
level\_1\_group:remove( clouds1 )
level\_1\_group:remove( clouds2 )
level\_1\_group:remove( clouds3 )
level\_1\_group:remove( clouds4 )
timer.cancel( bombz\_timer)
timer.cancel( spawn\_bombz\_timer)
timer.cancel ( spawn\_blue\_timer)
timer.cancel ( score\_timer)
tPrevious = nil
local function killAll( event )
director:changeScene("gameover")
end
timer.performWithDelay( 100, killAll, 1)
end
end
hero:addEventListener("collision", gameover)
return level\_1\_group;
end
Please, any help will be highly appreciated. I have been trying to solve this for a while now with no luck 
Thanks in advance [import]uid: 75258 topic_id: 14135 reply_id: 52247[/import]