I know i have to insert any created object to the scenceGroup to be able to let the composer remove it at the exit of the scene . and i able to do that with normal objects like background and images … but when i am trying to insert the soundFiles in the sceneGroup i always got errors also same happen with Image-Sheets .
now when i try to move from Scene to another ( level1 ) to GameOver or Menu . i still see the effect of the physics even when i removed the images related to them . also the sound still playing . and i got a black screen .
i tried to nil them and display.remove them but nothing help .
i have another 2 scenes ( Credits and Settings ) and when i go between them and the main menu scene it works perfectly because i am able to insert and remove Objects ( just some images as buttons ) and composer can see and remove them without issue
i suspect i have to add some objects to tables but is that also have to be with soundFiles . ?
So how can i manage that .
here is the code for level1
local composer = require( "composer" ) local scene = composer.newScene() -- ----------------------------------------------------------------------------------------------------------------- -- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called. -- ----------------------------------------------------------------------------------------------------------------- -- local forward references should go here display.setStatusBar(display.HiddenStatusBar) local physics = require("physics") physics.start() local soundPLay = false local Bats = {} -- table for the Enemies Bats spawned from top down local options = { effect = "slideDown", time = 800, } local sequences\_animatedBat = { -- consecutive frames sequence { name = "normalRun", frames = {1,2,3}, time = 500, loopCount = 0, loopDirection = "forward" }, { name = "falldown", frames = {4}, time = 500, loopCount = 0, loopDirection = "forward" }, } local sheetOptions = { width = 80, height = 75, numFrames = 5 } local sheet\_animatedBat = graphics.newImageSheet( "BatDownRun.png", sheetOptions ) local soundFile = audio.loadStream ("MainMusicGame.ogg") local soundFile2 = audio.loadSound ("BatHit.ogg") local ScoreToGameOver=0 local score=0 local sequences\_animatedSound = { { name = "mute", frames = {1}, time = 500, loopDirection = "forward" }, { name = "play", frames = {2}, time = 500, loopDirection = "forward" }, } local sheetOptionsSound = { width = 30, height = 30, numFrames = 2 } local sheet\_animatedSound = graphics.newImageSheet( "AnimatedSound.png", sheetOptionsSound ) local function onCollision( event ) if ( event.phase == "began" ) then print (ScoreToGameOver) system.vibrate() ScoreToGameOver = ScoreToGameOver + 1 --ScoreToGameOver.text = ScoreToGameOver elseif ( event.phase == "ended" ) then end end local function GoBack (event) composer.gotoScene ("menu", options) return true -- body end local function tapListenerStopMusic ( event ) -- here we have to add the tap function if soundPLay == true then audio.resume () soundPLay = false animatedSound:setSequence("play") elseif soundPLay == false then audio.pause () soundPLay = true animatedSound:setSequence("mute") end end -- ------------------------------------------------------------------------------- -- "scene:create()" function scene:create( event ) local sceneGroup = self.view -- Initialize the scene here. -- Example: add display objects to "sceneGroup", add touch listeners, etc. local score=0 local GoBackButton = display.newImage( "BackButton.png" ) GoBackButton.x=centerX GoBackButton.y=centerY+200 GoBackButton:addEventListener("tap", GoBack) sceneGroup:insert(GoBackButton) animatedSound = display.newSprite( sheet\_animatedSound, sequences\_animatedSound ) animatedSound.x=screenRight-20; animatedSound.y=screenTop+20; sceneGroup:insert(animatedSound) animatedSound:addEventListener ( "tap",tapListenerStopMusic ) local background1 = display.newImage ("FullBackGround.jpg") background1.x = centerX background1.y = centerY -100 background1.width = display.contentWidth\*2 background1.height = display.contentHeight \*2 sceneGroup:insert(background1) background1:toBack(); --transition.to ( background1, {time=12000,alpha=0.2 } ) local scoreText = display.newText(score,screenLeft+20,screenTop+20, native.systemFontBold, 30) scoreText:setTextColor(1, 0.6, 0.09) sceneGroup:insert(scoreText) Base = display.newRect(screenWidth, screenBottom-100, 600, 2) physics.addBody( Base, "static", {isSensor = true } ) sceneGroup:insert(Base) local function tapListener ( event ) -- here we have to add the tap function TheBats = event.target if TheBats.sequence == "normalRun" then TheBats:setSequence("falldown") TheBats:play() audio.play (soundFile2) transition.to ( TheBats, {time=500,x=centerX,y=centerY,alpha=0} ) score = score + 1 scoreText.text = score timer.performWithDelay(500,function() TheBats:removeSelf() end ) sceneGroup:insert(scoreText) elseif TheBats.sequence == "falldown" then TheBats:removeEventListener ( "tap" , tapListener) return true end end local function spawnBats() for i= 1,1 do Bats[i] = display.newSprite( sheet\_animatedBat, sequences\_animatedBat ); Bats[i].y= 0 Bats[i].x = 150 Bats[i]:play() sceneGroup:insert(Bats[i]) Bats[i].name = "TheBats" physics.addBody( Bats[i], "dynamic" ,{friction = 1} ,{isSensor = true }) transition.to ( Bats[i], {time=2500,x=150,y=600} ) Bats[i]:addEventListener( "collision", onCollision ) Bats[i]:addEventListener ( "tap",tapListener) end end local function GameOverNext( event) if score == 3 then composer.gotoScene ("GameOver") return true end end timer.performWithDelay(3000, spawnBats,12 ) Runtime:addEventListener( "enterFrame", GameOverNext ) Base:addEventListener( "collision", onCollision ) end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is still off screen (but is about to come on screen). --sceneGroup:insert(sheetOptionsSound) --sceneGroup:insert(sheet\_animatedSound) --sceneGroup:insert(scoreText) --sceneGroup:insert(sequences\_animatedSound) --sceneGroup:insert(soundFile2) --sceneGroup:insert(sheetOptions) --sceneGroup:insert(sheet\_animatedBat) --sceneGroup:insert(sequences\_animatedBat) --sceneGroup:insert(animatedSound) elseif ( phase == "did" ) then -- Called when the scene is now on screen. -- Insert code here to make the scene come alive. -- Example: start timers, begin animation, play audio, etc. audio.play(soundFile) local ads = require( "ads" ) bannerAppID = "ca-app-pub-xxx" --for your Android banner interstitialAppID = "ca-app-pub-xxx" --for your Android interstitial --end local adProvider = "admob" ads.init( adProvider, "ca-app-pub-xxx", adListener ) ads.show( "banner", { x=0, y=100000 } ) --sceneGroup:insert(adProvider) if ( ads.isLoaded("interstitial") ) then ads.show( "interstitial", { appId=interstitialAppID } ) end end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is on screen (but is about to go off screen). -- Insert code here to "pause" the scene. -- Example: stop timers, stop animation, stop audio, etc. elseif ( phase == "did" ) then -- Called immediately after scene goes off screen. end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's view ("sceneGroup"). -- Insert code here to clean up the scene. -- Example: remove display objects, save state, etc. local sheet\_animatedBat=nil local sheet\_animatedSound=nil local soundFile=nil local soundFile2=nil display.remove( soundFile ) display.remove(soundFile2) display.remove(sheet\_animatedSound) display.remove(sheet\_animatedBat) -- is a faster way of doing this: for i= 1,1 do Bats[i]:removeSelf() end TheBats:removeSelf() sceneGroup.remove(spawnBats) sceneGroup.remove(animatedSound) sceneGroup.remove(background1) sceneGroup.remove(scoreText) sceneGroup.remove(Base) sceneGroup.remove(Bats[i]) composer.removeScene( "Level1",true ) composer.removeScene( "main" ) composer.removeScene( "menu" ) end -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene
