How can I remove the whole scene so none of the objects are displayed when exiting a scene?
Do I have to remove every object individually?
How can I remove the whole scene so none of the objects are displayed when exiting a scene?
Do I have to remove every object individually?
As long as you have inserted the objects into the scene group they will be removed automatically when the scene is destroyed.
Things like Runtime listeners have to be manually removed, but display objects are handled for you.
Jay
It isn’t removing anything and it is all in my createScene function
here is all my code for the scene
<code>
–start
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
storyboard.reloadScene()
local physics = require (“physics”)
physics.start()
physics.setGravity( 0 , 70)
display.setStatusBar( display.HiddenStatusBar )
local widget = require (“widget”)
storyboard.reloadScene(“game”)
function scene:createScene( event )
local group = self.view
score = 0
local scoreNumber = display.newText(score, 145, 55, nil, 50)
scoreNumber.xScale = 1
scoreNumber.yScale = 1
local bottomBlock = display.newImageRect(“topBlock.png”,320,0)
bottomBlock.x = 0
bottomBlock.y = 465
physics.addBody(bottomBlock, “static”, {density=1, bounce=0.1, friction=0.2})
local tileback = display.newImageRect( “tile.png”,325,480)
tileback:setReferencePoint(display.BottomLeftReferencePoint)
tileback.x = 0
tileback.y = 480
tileback.speed = 2
local tileback1 = display.newImageRect( “tile.png”,325,480)
tileback1:setReferencePoint(display.BottomLeftReferencePoint)
tileback1.x = 320
tileback1.y = 480
tileback1.speed = 2
local myPics = {“onePole4.png”, “onePole3.png”, “onePole2.png”, “onePole1.png” }
local idx = math.random(#myPics)
local pic = display.newImage(myPics[idx])
pic.width = 30
pic.height = 240
pic:setReferencePoint(display.BottomLeftReferencePoint)
pic.speed = 3
pic.x = 0
pic.y = 480
local myPics = {“onePole4.png”, “onePole3.png”, “onePole2.png”, “onePole1.png” }
local idx = math.random(#myPics)
local pic1 = display.newImage(myPics[idx])
pic1.width = 30
pic1.height = 140
pic1:setReferencePoint(display.BottomLeftReferencePoint)
pic1.speed = 3
pic1.x = 300
pic1.y = 480
local myPics = {“onePole4.png”, “onePole3.png”, “onePole2.png”, “onePole1.png” }
local idx = math.random(#myPics)
local pic2 = display.newImage(myPics[idx])
pic2.width = 30
pic2.height = 40
pic2:setReferencePoint(display.BottomLeftReferencePoint)
pic2.speed = 3
pic2.x = 600
pic2.y = 480
local myPics = {“onePole4.png”, “onePole3.png”, “onePole2.png”, “onePole1.png” }
local idx = math.random(#myPics)
local pic3 = display.newImage(myPics[idx])
pic3.width = 30
pic3.height = 300
pic3:setReferencePoint(display.BottomLeftReferencePoint)
pic3.speed = 3
pic3.x = 900
pic3.y = 480
–random pics
–playerspritesheet
local sheetData = {
width =150,
height=150,
numFrames=4,
sheetContentWidth=600,
sheetContentHeight=150,
}
local robin = graphics.newImageSheet( “robin.png”, sheetData )
local sequenceData = {
{ name=“normalRun”, start=1, count=4, time=500 },
}
local robin = display.newSprite( robin, sequenceData )
robin.x = display.contentWidth/4
robin.y = display.contentHeight/2
robin.y = 240
robin.x = 120
robin:play()
robin:scale(0.5,0.5)
----mostfunctions
function tapToPlay(event)
if event.phase == “began” then
local function startScore()
local function pointScore()
score = score + 1
scoreNumber.text = score
end
scorer = timer.performWithDelay(1500,pointScore,-1)
end
timer.performWithDelay(2000,startScore)
end
end
function tapToRemove(event)
if event.phase == “began” then
Runtime:removeEventListener(“touch”, tapToPlay)
end
end
timer.performWithDelay(1,tapToPlay)
function touchScreen(event)
if event.phase == “began” then
robin.rotation = -20
local turdFlap = audio.loadSound(“turdFlap.wav”)
audio.play(turdFlap)
physics.addBody(robin, “dynamic”, {density=1, bounce=0.1, friction=0.2,radius=1})
Runtime:addEventListener(“enterFrame”, robin)
robin:applyForce(0,-5,robin.x,robin.y)
end
if event.phase == “ended” then
robin.rotation = 10
Runtime:removeEventListener(“enterFrame”, robin)
end
end
local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5
local function onCollision (event)
if event.phase == “began” then
local function physicsStop()
physics.stop()
end
timer.performWithDelay(50, physicsStop)
--okButton
local ok
local function onokRelease()
storyboard.gotoScene(“menu”,“fade”,400)
storyboard.removeScene(“game”)
local slideMenu = audio.loadSound(“flush.wav”)
audio.play(slideMenu)
end
ok = widget.newButton{
defaultFile=“ok.png”,
overFile=“ok1.png”,
width=40, height=40,
onRelease = onokRelease
}
ok.x = display.contentWidth*0.25
ok.y = display.contentHeight - 200
physics.addBody(ok,“static”,{density=1, bounce=0.5, friction=0.2})
--okButton
timer.pause (scorer)
local birdcollide = audio.loadSound(“turdSplat.wav”)
audio.play(birdcollide)
pic.speed = 0
pic1.speed = 0
pic2.speed = 0
pic3.speed=0
tileback.speed=0
tileback1.speed=0
Runtime:removeEventListener(“touch”,touchScreen)
end
end
local function scrolltile(self,event)
if self.x < -320 then
self.x = 320
else
self.x = self.x - self.speed
end
end
pic.enterFrame = scrolltile
Runtime:addEventListener(“enterFrame”, pic)
pic1.enterFrame = scrolltile
Runtime:addEventListener(“enterFrame”, pic1)
pic2.enterFrame = scrolltile
Runtime:addEventListener(“enterFrame”, pic2)
pic3.enterFrame = scrolltile
Runtime:addEventListener(“enterFrame”, pic3)
tileback.enterFrame = scrolltile
Runtime:addEventListener(“enterFrame”, tileback)
tileback1.enterFrame = scrolltile
Runtime:addEventListener(“enterFrame”, tileback1)
Runtime:addEventListener(“collision”, onCollision)
Runtime:addEventListener(“touch”, touchScreen)
Runtime:addEventListener(“touch”, tapToPlay)
Runtime:addEventListener(“touch”, tapToRemove)
group:insert(bottomBlock)
group:insert(tileback)
group:insert(tileback1)
group:insert(robin)
end
function scene:enterScene( event )
local group = self.view
end
function scene:exitScene( event )
local group = self.view
end
function scene:destroyScene( event )
local group = self.view
end
scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene )
scene:addEventListener( “destroyScene”, scene )
return scene
<code>
I maybe missing something but it seems no display objects are inserted into the scene group. As Jay said, unless you do that, Storyboard would not going to be able to removed the,.
Good luck.
Mo
Ps: there is no need to repeat local myPics table…
it is in the createScene group
I see these items being inserted:
group:insert(bottomBlock)
group:insert(tileback)
group:insert(tileback1)
group:insert(robin)
But you are creating other things I don’t see getting inserted.
This block of code:
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
storyboard.reloadScene()
local physics = require (“physics”)
physics.start()
physics.setGravity( 0 , 70)
display.setStatusBar( display.HiddenStatusBar )
local widget = require (“widget”)
storyboard.reloadScene(“game”)
only executes once, unless you do a storyboard.removeScene() call some where in another module. I’m unsure why you are trying to reload the scene twice (that doesn’t exist yet). You probably should also hide your status bar in your main.lua rather than in a scene file.
Now assuming all of your display items are in “group” and you’re using your destoryScene() to take care disposing audio and cancel all of those runtime listeners (really those should be in enterScene and cancelled in exitScene), then from another scene, you can call storyboard.removeScene(“scenenametoremove”) and it will completely dump the module.
Rob
wow I really don’t know how to use storyboard then. I have nothing in my destoryScene or any other scene for that matter… I only have all my stuff in the createScene agh I want to make an app I want this mess cleared I don’t know how to do it.
You are not too far off. There are rarely things you have to do in destroyScene. But most of the time people are not adding touch handlers to the Runtime object, but to the display objects. Think of createScene as the place to create anything that shows (other than native.* objects). If you are adding touch handlers and such directly to the object, you do that in createScene as well.
You can load audio there, but if you do, you have to dispose of it in destroyScene. createScene and destroyScene are kind of pairs. They go together. Now Corona SDK will clean up your display objects for you, and if you were adding those touch handlers to display objects they would be cleaned up too. This is why there isn’t much to do in destroyScene
Normally I would start Runtime listeners in enterScene and remove them in it’s mate, exitScene as they always execute in pairs.
where would I put the onCollision function?
Typically you’d put that up above the scene:createScene() function – you can stick all the functions for that scene up there. I’ve found the fewer lines of code inside createScene() and enterScene() the better (because then you can more easily reuse those functions and maintenance in general is easier that way).
Jay
ahhh ok this is all making a lot more sense now. Where would I remove event listeners? btw thanks a ton!
As Jay said, the function probably should be outside of any of the scene event functions, but the code to turn the collision handler (event listener) on can be done in enterScene. You really don’t want physics starting to do things until after the scene is on the screen. Then you would remove it in exitScene.
is there a way I can completely remove a function?
You do not need to worry about removing functions. They don’t take up much memory and don’t need freed up. When a scene is completely removed, all of the code of that module is also removed as well.
As long as you have inserted the objects into the scene group they will be removed automatically when the scene is destroyed.
Things like Runtime listeners have to be manually removed, but display objects are handled for you.
Jay
It isn’t removing anything and it is all in my createScene function
here is all my code for the scene
<code>
–start
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
storyboard.reloadScene()
local physics = require (“physics”)
physics.start()
physics.setGravity( 0 , 70)
display.setStatusBar( display.HiddenStatusBar )
local widget = require (“widget”)
storyboard.reloadScene(“game”)
function scene:createScene( event )
local group = self.view
score = 0
local scoreNumber = display.newText(score, 145, 55, nil, 50)
scoreNumber.xScale = 1
scoreNumber.yScale = 1
local bottomBlock = display.newImageRect(“topBlock.png”,320,0)
bottomBlock.x = 0
bottomBlock.y = 465
physics.addBody(bottomBlock, “static”, {density=1, bounce=0.1, friction=0.2})
local tileback = display.newImageRect( “tile.png”,325,480)
tileback:setReferencePoint(display.BottomLeftReferencePoint)
tileback.x = 0
tileback.y = 480
tileback.speed = 2
local tileback1 = display.newImageRect( “tile.png”,325,480)
tileback1:setReferencePoint(display.BottomLeftReferencePoint)
tileback1.x = 320
tileback1.y = 480
tileback1.speed = 2
local myPics = {“onePole4.png”, “onePole3.png”, “onePole2.png”, “onePole1.png” }
local idx = math.random(#myPics)
local pic = display.newImage(myPics[idx])
pic.width = 30
pic.height = 240
pic:setReferencePoint(display.BottomLeftReferencePoint)
pic.speed = 3
pic.x = 0
pic.y = 480
local myPics = {“onePole4.png”, “onePole3.png”, “onePole2.png”, “onePole1.png” }
local idx = math.random(#myPics)
local pic1 = display.newImage(myPics[idx])
pic1.width = 30
pic1.height = 140
pic1:setReferencePoint(display.BottomLeftReferencePoint)
pic1.speed = 3
pic1.x = 300
pic1.y = 480
local myPics = {“onePole4.png”, “onePole3.png”, “onePole2.png”, “onePole1.png” }
local idx = math.random(#myPics)
local pic2 = display.newImage(myPics[idx])
pic2.width = 30
pic2.height = 40
pic2:setReferencePoint(display.BottomLeftReferencePoint)
pic2.speed = 3
pic2.x = 600
pic2.y = 480
local myPics = {“onePole4.png”, “onePole3.png”, “onePole2.png”, “onePole1.png” }
local idx = math.random(#myPics)
local pic3 = display.newImage(myPics[idx])
pic3.width = 30
pic3.height = 300
pic3:setReferencePoint(display.BottomLeftReferencePoint)
pic3.speed = 3
pic3.x = 900
pic3.y = 480
–random pics
–playerspritesheet
local sheetData = {
width =150,
height=150,
numFrames=4,
sheetContentWidth=600,
sheetContentHeight=150,
}
local robin = graphics.newImageSheet( “robin.png”, sheetData )
local sequenceData = {
{ name=“normalRun”, start=1, count=4, time=500 },
}
local robin = display.newSprite( robin, sequenceData )
robin.x = display.contentWidth/4
robin.y = display.contentHeight/2
robin.y = 240
robin.x = 120
robin:play()
robin:scale(0.5,0.5)
----mostfunctions
function tapToPlay(event)
if event.phase == “began” then
local function startScore()
local function pointScore()
score = score + 1
scoreNumber.text = score
end
scorer = timer.performWithDelay(1500,pointScore,-1)
end
timer.performWithDelay(2000,startScore)
end
end
function tapToRemove(event)
if event.phase == “began” then
Runtime:removeEventListener(“touch”, tapToPlay)
end
end
timer.performWithDelay(1,tapToPlay)
function touchScreen(event)
if event.phase == “began” then
robin.rotation = -20
local turdFlap = audio.loadSound(“turdFlap.wav”)
audio.play(turdFlap)
physics.addBody(robin, “dynamic”, {density=1, bounce=0.1, friction=0.2,radius=1})
Runtime:addEventListener(“enterFrame”, robin)
robin:applyForce(0,-5,robin.x,robin.y)
end
if event.phase == “ended” then
robin.rotation = 10
Runtime:removeEventListener(“enterFrame”, robin)
end
end
local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5
local function onCollision (event)
if event.phase == “began” then
local function physicsStop()
physics.stop()
end
timer.performWithDelay(50, physicsStop)
--okButton
local ok
local function onokRelease()
storyboard.gotoScene(“menu”,“fade”,400)
storyboard.removeScene(“game”)
local slideMenu = audio.loadSound(“flush.wav”)
audio.play(slideMenu)
end
ok = widget.newButton{
defaultFile=“ok.png”,
overFile=“ok1.png”,
width=40, height=40,
onRelease = onokRelease
}
ok.x = display.contentWidth*0.25
ok.y = display.contentHeight - 200
physics.addBody(ok,“static”,{density=1, bounce=0.5, friction=0.2})
--okButton
timer.pause (scorer)
local birdcollide = audio.loadSound(“turdSplat.wav”)
audio.play(birdcollide)
pic.speed = 0
pic1.speed = 0
pic2.speed = 0
pic3.speed=0
tileback.speed=0
tileback1.speed=0
Runtime:removeEventListener(“touch”,touchScreen)
end
end
local function scrolltile(self,event)
if self.x < -320 then
self.x = 320
else
self.x = self.x - self.speed
end
end
pic.enterFrame = scrolltile
Runtime:addEventListener(“enterFrame”, pic)
pic1.enterFrame = scrolltile
Runtime:addEventListener(“enterFrame”, pic1)
pic2.enterFrame = scrolltile
Runtime:addEventListener(“enterFrame”, pic2)
pic3.enterFrame = scrolltile
Runtime:addEventListener(“enterFrame”, pic3)
tileback.enterFrame = scrolltile
Runtime:addEventListener(“enterFrame”, tileback)
tileback1.enterFrame = scrolltile
Runtime:addEventListener(“enterFrame”, tileback1)
Runtime:addEventListener(“collision”, onCollision)
Runtime:addEventListener(“touch”, touchScreen)
Runtime:addEventListener(“touch”, tapToPlay)
Runtime:addEventListener(“touch”, tapToRemove)
group:insert(bottomBlock)
group:insert(tileback)
group:insert(tileback1)
group:insert(robin)
end
function scene:enterScene( event )
local group = self.view
end
function scene:exitScene( event )
local group = self.view
end
function scene:destroyScene( event )
local group = self.view
end
scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene )
scene:addEventListener( “destroyScene”, scene )
return scene
<code>
I maybe missing something but it seems no display objects are inserted into the scene group. As Jay said, unless you do that, Storyboard would not going to be able to removed the,.
Good luck.
Mo
Ps: there is no need to repeat local myPics table…
it is in the createScene group
I see these items being inserted:
group:insert(bottomBlock)
group:insert(tileback)
group:insert(tileback1)
group:insert(robin)
But you are creating other things I don’t see getting inserted.
This block of code:
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
storyboard.reloadScene()
local physics = require (“physics”)
physics.start()
physics.setGravity( 0 , 70)
display.setStatusBar( display.HiddenStatusBar )
local widget = require (“widget”)
storyboard.reloadScene(“game”)
only executes once, unless you do a storyboard.removeScene() call some where in another module. I’m unsure why you are trying to reload the scene twice (that doesn’t exist yet). You probably should also hide your status bar in your main.lua rather than in a scene file.
Now assuming all of your display items are in “group” and you’re using your destoryScene() to take care disposing audio and cancel all of those runtime listeners (really those should be in enterScene and cancelled in exitScene), then from another scene, you can call storyboard.removeScene(“scenenametoremove”) and it will completely dump the module.
Rob
wow I really don’t know how to use storyboard then. I have nothing in my destoryScene or any other scene for that matter… I only have all my stuff in the createScene agh I want to make an app I want this mess cleared I don’t know how to do it.