Remove object once idle and reset it.

I would like my orb to remove itself and reset to throw another one once it has stopped moving. The way I have it below is it collides and a timer removes it after a few seconds after a collision and transitions back to the start point on the screen. But this way isn’t that efficient if the orb is still moving

and it only works once, I can throw the object a second time, but the collsion code doesn’t work on the next orb thrown. What am I doing wrong?

  
 local function resetOrb()  
 orb.bodyType = "kinematic"  
 orb.x = 30  
 orb.y = -140  
 orb:setLinearVelocity( 0, 0 ) orb.angularVelocity = 0 end  
 resetOrb()   
  
 -- Camera follow  
 local function moveCamera()  
 if (orb.x \> 80 and orb.x \< 1100) then  
 game.x = -orb.x + 100  
 end  
 end  
 Runtime:addEventListener( "enterFrame", moveCamera )  
  
   
 if gameLives \>= 1 and isGameOver == false then  
 local function dropOrb ( event )  
 if ( not gameIsActive ) and ( event.phase == "began" ) then  
 gameIsActive = true  
 -- audio.play( knockSound )  
 orb.x = event.x - game.x  
 orb.y = event.y  
 -- change body type to dynamic, so gravity affects it  
 orb.bodyType = "dynamic"  
 instructionLabel.text=" "  
 gameLives = gameLives - 1  
 l.text=gameLives   
 print( gameLives )  
 end  
 end  
  
 -- listener to the rect, for creating new orb  
 rect:addEventListener( "touch", dropOrb )   
  
 elseif gameLives \< 0 then  
 gameLives = 0  
 isGameOver = true  
  
 local GameOverLabel = display.newText( " ", screenW\*.5, screenH\*.5, native.systemFont, 28 )  
 GameOverLabel:setTextColor( 255, 0, 0, 255 )   
 game:insert( GameOverLabel )  
 GameOverLabel.text="Game OVer"  
  
 end  
  
 local onOrbCollision = function( self, event )  
 if event.phase == "began" and event.other.myName == "trashCan" or event.other.myName == "board" or event.other.myName == "rat"and gameIsActive then  
 local function removeOrb( event )   
 resetOrb()  
 gameIsActive = false  
 instructionLabel.text="Drop Here"  
 transition.to( game, { time=3000, delay=1500, x=0, y=0, xScale = 1, yScale = 1 } )  
 return true  
 end   
 self:removeEventListener( "postCollision", self )   
 timer.performWithDelay( 2000, removeOrb)   
  
 elseif event.other.myName == "ground" and gameIsActive then   
 local function removeOrb( event )   
 resetOrb()  
 game.x = 0  
 game.y = 0  
 gameIsActive = false  
 instructionLabel.text="Drop Here"  
 transition.to( game, { time=2000, delay=1500, x=0, y=0, xScale = 1, yScale = 1 } )  
 return true  
 end   
 self:removeEventListener( "postCollision", self )   
 timer.performWithDelay( 500, removeOrb)   
 elseif event.other.myName == "ground2" and gameIsActive then   
 local function removeOrb( event )   
 resetOrb()  
 game.x = 0  
 game.y = 0  
 gameIsActive = false  
 instructionLabel.text="Drop Here"  
 transition.to( game, { time=4000, delay=1500, x=0, y=0, xScale = 1, yScale = 1 } )  
 return true  
 end   
 self:removeEventListener( "postCollision", self )   
 timer.performWithDelay( 500, removeOrb)   
 elseif event.other.myName == "leftbarrier" and gameIsActive then   
 local function removeOrb( event )   
 resetOrb()  
 game.x = 0  
 game.y = 0  
 gameIsActive = false  
 instructionLabel.text="Drop Here"  
 transition.to( game, { time=300, delay=1500, x=0, y=0, xScale = 1, yScale = 1 } )  
 return true  
 end   
 self:removeEventListener( "postCollision", self )   
 timer.performWithDelay( 500, removeOrb)   
 end  
 end  
 orb.postCollision = onOrbCollision  
 orb:addEventListener( "postCollision", orb )  
  

Thanks Again,

Dan [import]uid: 78446 topic_id: 14135 reply_id: 314135[/import]

Do you have any plug and play code you could share with us? I can’t see anything obvious here - at first I thought perhaps a gameIsActive issue but that appears to be in order.

Have you tried working out where the issue lies using print statements? [import]uid: 52491 topic_id: 14135 reply_id: 52096[/import]

This and my other post is basically the whole thing. what’s missing is just my images layout stuff.

In my- if gameLives >= 1 and isGameOver == false then,
the print works when I place the orb both times.

I put a (print) in each- local function removeOrb and it prints the first time it hits one of them, it resets the orb, I can place the orb again, but it doesn’t print the second time through

I have the onRatCollision right after the onOrbCollision and the (print) in both collisions print the first time through( as long as I hit both), in my second orb placement only the collisions register in this onRatCollision section not the onOrbCollision.

[code]
local function onRatCollision ( self, event )
if Rathita == 0 then
if ( event.force > 45.0 ) then
self:stopAtFrame(2)
Rathita = 1
score = score + 15
t.text=score
end
end
if Rathita == 1 then
if ( event.force > 35.0 ) then
print( “Rat Kill Event X-”…event.x…“Y-”…event.y )
self:stopAtFrame(3)
points=150
score = score + points
t.text=score
kills = kills + 1
print( “kills:”…kills )
–destructionScore = display.newText(points, event.x, event.y, native.systemFont, 40 )
–destructionScore:setTextColor( 255, 0, 0)
–game:insert( destructionScore )
local function removeRat( event )
print( “Rat Kills in remove -”…kills )
print( “points in remove -”…points )
self:removeSelf()
– destructionScore=""
end
self:removeEventListener( “postCollision”, self )
timer.performWithDelay( 600, removeRat)
end
end
end

– Set table listeners in each rat to check for collisions
Rat1.postCollision = onRatCollision
Rat1:addEventListener( “postCollision”, Rat1 )

Rat2.postCollision = onRatCollision
Rat2:addEventListener( “postCollision”, Rat2 )
[/code] [import]uid: 78446 topic_id: 14135 reply_id: 52219[/import]

could be wrong on this cause you have a lot going on, and it’s a bit hard to follow cause I can’t mess with it, but by the sound of what you describe I would set event.phase = “ended” before you reset the orb and see where that gets you. You aren’t removing the event listener it looks like so my guess is my moving the object when the event.phase began the “collision” continues indefinitely. [import]uid: 19176 topic_id: 14135 reply_id: 52227[/import]

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 :frowning:

Thanks in advance [import]uid: 75258 topic_id: 14135 reply_id: 52247[/import]

All you need to do is create another function say unload() and within that you can remove your event listeners and sprites before you change scene. If you are declaring the sprites locally though and adding them to the returned displayGroup as you should you won’t need to nil them out. Just removeEventListener in that unload() function and you should be good. Use debugger to find issues. [import]uid: 19176 topic_id: 14135 reply_id: 52249[/import]

Thanks ne.hannah for your response. The unload() function you speak of is the gameover() function I have in the code, would it be it? If you look closely I have all of the code that removes the listener when lives == 0, yet I get every time the function is called I get an error like this: attempt to perform arithmetic on field 'x' (a nil value) stack traceback: .

It seems like the animation (translate:(480*2, 0) command isn’t resetting it self, therefore it’s giving me the error, I think. I am not sure how to solve this problem.

I hope I made any sense.

Thanks for you help [import]uid: 75258 topic_id: 14135 reply_id: 52254[/import]

if u post a zip with everything it will be easier to debug, what I see now is that you don’t remove the moveCamera event listener. [import]uid: 19176 topic_id: 14135 reply_id: 52318[/import]

Hello ne.hannah,
how do I send you the zip file in here? Sorry for all these dumb question, but again I am new here as well :slight_smile:
[import]uid: 75258 topic_id: 14135 reply_id: 52400[/import]

upload to 3rd party site and link, dropbox, megaupload, there’s a bunch that will let u share a link to DL. [import]uid: 19176 topic_id: 14135 reply_id: 52420[/import]

Now if I have more than 1 life, any idea why this only works once. The first time the orb hits something it removes it, the scene transitions back to the starting point and I can drop the orb in again but no collision and no print() in the onOrbCollision function, so it won’t remove the orb and reset or end/complete.

[code]

--------- Drop Orb

local function resetOrb()
orb.bodyType = “kinematic”
orb.x = 30
orb.y = -140
orb:setLinearVelocity( 0, 0 )
orb.angularVelocity = 0
end
resetOrb()

– Camera follow
local function moveCamera()
if (orb.x > 80 and orb.x < 1100) then
game.x = -orb.x + 100
end
end
Runtime:addEventListener( “enterFrame”, moveCamera )

local function newRound()
print(“new Round, game lives”… gameLives )
if gameLives == 0 then

print(“No game lives”)
isGameOver = true
local GameOverLabel = display.newText( " “, screenW*.5, screenH*.5, native.systemFont, 28 )
GameOverLabel:setTextColor( 255, 0, 0, 255 )
game:insert( GameOverLabel )
GameOverLabel.text=“Game OVer” --add director to menu screen
instructionLabel.text=” "

physics.stop()–make an unload

Runtime:removeEventListener( “enterFrame”, clawmove )
Runtime:removeEventListener( “enterFrame”, moveCamera )
Runtime:removeEventListener( “touch”, onScreenTouch )
end

if kills == 2 then
print(“2 kills”)
isGameOver = true
local GameWinLabel = display.newText( " “, screenW*.5, screenH*.5, native.systemFont, 28 )
GameWinLabel:setTextColor( 255, 0, 0, 255 )
game:insert( GameWinLabel )
GameWinLabel.text=“Level Complete”–add director to menu screen
instructionLabel.text=” "
physics.stop() --make an unload function

Runtime:removeEventListener( “enterFrame”, clawmove )
Runtime:removeEventListener( “enterFrame”, moveCamera )
Runtime:removeEventListener( “touch”, onScreenTouch )

end
if gameLives >= 1 and isGameOver == false and kills < 2 then
print(“Game Continue”)
resetOrb()
local function dropOrb ( event )
if ( gameIsActive == false ) and ( event.phase == “began” ) then
gameIsActive = true
– audio.play( knockSound )
orb.x = event.x - game.x
orb.y = event.y
– change body type to dynamic, so gravity affects it
orb.bodyType = “dynamic”
instructionLabel.text=" "
gameLives = gameLives - 1
l.text=gameLives
print( gameLives )
end
end

– listener to the rect, for creating new orb
rect:addEventListener( “touch”, dropOrb )
end

end
newRound()

local onOrbCollision = function( self, event )
if event.other.myName == “trashCan” or event.other.myName == “board” or event.other.myName == "rat"and gameIsActive then
local function removeOrb1( event )
newRound()
print(“go to newRound items”)
gameIsActive = false
instructionLabel.text=“Drop Here”
transition.to( game, { time=3000, delay=1500, x=0, y=0, xScale = 1, yScale = 1 } )
return true
end
self:removeEventListener( “postCollision”, self )
timer.performWithDelay( 2000, removeOrb1)

elseif event.other.myName == “ground” and gameIsActive then

local function removeOrb2( event )
print(“go to newRound GROUND”)
newRound()
gameIsActive = false
instructionLabel.text=“Drop Here”
transition.to( game, { time=2000, delay=1500, x=0, y=0, xScale = 1, yScale = 1 } )
return true
end
self:removeEventListener( “postCollision”, self )
timer.performWithDelay( 500, removeOrb2)
newRound()
return true

elseif event.other.myName == “ground2” and gameIsActive then
local function removeOrb3( event )
print(“go to newRound GROUND2”)
newRound()
gameIsActive = false
instructionLabel.text=“Drop Here”
transition.to( game, { time=4000, delay=1500, x=0, y=0, xScale = 1, yScale = 1 } )
return true

end
self:removeEventListener( “postCollision”, self )
timer.performWithDelay( 500, removeOrb3)

elseif event.other.myName == “leftbarrier” and gameIsActive then
local function removeOrb4( event )
print(“go to newRound LEFT BARRIER”)
newRound()
gameIsActive = false
instructionLabel.text=“Drop Here”
transition.to( game, { time=300, delay=1500, x=0, y=0, xScale = 1, yScale = 1 } )
return true

end
self:removeEventListener( “postCollision”, self )
timer.performWithDelay( 500, removeOrb4)

end
end

orb.postCollision = onOrbCollision
orb:addEventListener( “postCollision”, orb )
[/code] [import]uid: 78446 topic_id: 14135 reply_id: 52427[/import]

Hello ne.hannah, here is the link to the zip file. I hope this will help finding the error.
Thank you sooo much for you help.

http://www.magadistudio.com/files/ [import]uid: 75258 topic_id: 14135 reply_id: 52436[/import]

Hey pdichone, I took a look and fixed some stuff, but my assessment is restart from scratch and work in sections. I was missing tons of images which caused crashes and after that the problem seemed to be the way the entire game was structured as far as creating itself and destroying itself. The Runtime listeners where declared locally in another function so they couldn’t be removed from outside of it, but when declare globally other errors pop’d up. I spent over 30 minutes tweeking stuff but in the end I would say restart. You have a lot of code that isn’t doing much and is hard to read so try to be more organized next time as well. I know you are new to programming but this is one of the things they teach you, you might know where everything is in the code, but everyone else starts from scratch looking at what you wrote. Its not a big deal. Everyone starts somewhere. Sorry I couldn’t be of more help but I am pretty busy today. [import]uid: 19176 topic_id: 14135 reply_id: 52481[/import]

Hello ne.hannah,

Thanks for the feedback and for taking the time to take a look at my code. Highly appreciated! You are right, I will have to restart the entire project just because everything is pretty disorganized so far… my fault.

Thanks again for all your help.
[import]uid: 75258 topic_id: 14135 reply_id: 52517[/import]