**UPDATE ON POST BELOW**
Okay, so I decided to carry on using storyboard but create my objects outside of the function ‘scene:createScene( event )’
I decided that this way I can get rid of them anytime I choose and know they have been destroyed. What I also did was add all my objects into a reload() function and then add reload() to ‘function scene:enterScene( event )’, this way it reloads the entire scene so everything starts from the beginning.
**END OF UPDATE**
Thanks Brent. It’s good to be able to see the memory in use, that definitely helps me to see if I’m releasing objects.
I thought there would likely be a better way to move the clouds rather than with all the timers I had…thanks.
Strange you should mention those videos as I was watching the 7th one when I looked at your message!
There is one thing I am really struggling with though and it’s incredibly frustrating because I would never expect to spend nearly 2 days on trying to figure out what happens to objects when a change of scene takes place!
Initially I had a project that had just 2 scenes, the first scene had a bit of animation going on (the clouds moving) a couple of other images, some music playing and a button that when tapped went to scene 2 which literally just had a background image and a button that went back to scene 1.
As it was so simple, I didn’t really think it was possible for it to go wrong…famous last words. After spending hours and hours on it I noticed a couple of things. Firstly this is how I thought scenes worked:
Scene 1 loads and shows necessary objects etc, on button press to go to scene 2, transition takes place and once scene 2 is on the screen, Scene 1 is automatically deleted (all objects, tables etc etc). Exactly the same thing happens when the button on Scene 2 is pressed but in reverse. So essentially when going to any seen irrelevant of whether it’s been shown before it should load back into memory completely fresh.
I’m obviously wrong with the above because as I said earlier I noticed a couple of things. Firstly I added your memory code to allow me to see what was happening with my scene 1 objects or at least to see if they were removed once scene 2 was showing and to my surprise the memory usage stayed exactly the same as did texture. So I decided to remove some objects manually from scene 1 using a timer of 1 second so that when the button was pressed to goto scene 2 the timer would fire a function to remove about 4 graphical items. Once I did this I could see that this time the memory and texture was reduced as I expected.
I also noticed that when I went to scene 1 from scene 2, the music I had set to play in scene 1 never started again after going to scene 2?! It plays when I first launch the app and stops as I requested when going to scene 2 but as soon as I go back the music is not playing.
So with all the above in mind I decided to create a really simple 2 scene project. Scene 1 has a background, music playing and a button which takes me to scene 2. Scene 2 has nothing but a background and a button which takes me to scene 1. Using the scene templates this is how my code looks but still scene 1 never seems to unload and the music never starts again on going back to scene 1 from scene 2?
Main.lua
local storyboard = require "storyboard"
-- load scenetemplate.lua
storyboard.gotoScene( "scene1", "fade", 800 )
Scene1.lua
----------------------------------------------------------------------------------
--
-- scenetemplate.lua
--
----------------------------------------------------------------------------------
local storyboard = require( "storyboard" )
local widget = require "widget"
local scene = storyboard.newScene()
local bgImage, playButton, homeScreenMusicChannel
----------------------------------------------------------------------------------
local function gotoScene2()
storyboard.gotoScene( "Scene2", "crossFade", 800 )
end
---------------------------------------------------------------------------------
-- BEGINNING OF YOUR IMPLEMENTATION
---------------------------------------------------------------------------------
-- Called when the scene's view does not exist:
function scene:createScene( event )
local group = self.view
bgImage = display.newImageRect( "Home-Page-BG.png", 320, 568 )
bgImage:setReferencePoint( display.CenterReferencePoint )
bgImage.x = display.contentCenterX
bgImage.y = display.contentCenterY
group:insert( bgImage )
playButton = widget.newButton{
default = "Play-Button.png",
over = "Play-Button.png",
onPress = playButtonPress,
onEvent = buttonHandler,
id = "playButton",
width = 198,
height = 69.5
}
playButton.x = 160; playButton.y = 250
group:insert( playButton )
function playButton:tap( event )
audio.fadeOut({channel=1, time=1400})
timer.performWithDelay(1400, removeAudio, 1)
gotoHomeScreenTimer = timer.performWithDelay(1000, gotoScene2, 1)
end
end
-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
playButton:addEventListener( "tap", button )
homeScreenMusicChannel = audio.loadStream( "Home Screen Music.mp3" )
homeScreenMusicChannel = audio.play( homeScreenMusicChannel, { channel=1} )
end
-- Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view
--function removeAudio (event)
audio.dispose( homeScreenMusicChannel )
homeScreenMusicChannel = nil
-----------------------------------------------------------------------------
-- INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.)
-----------------------------------------------------------------------------
end
-- Called prior to the removal of scene's "view" (display group)
function scene:destroyScene( event )
local group = self.view
display.remove( playButton )
playButton = nil
-----------------------------------------------------------------------------
-- INSERT code here (e.g. remove listeners, widgets, save state, etc.)
-----------------------------------------------------------------------------
end
---------------------------------------------------------------------------------
-- END OF YOUR IMPLEMENTATION
---------------------------------------------------------------------------------
-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )
-- "enterScene" event is dispatched whenever scene transition has finished
scene:addEventListener( "enterScene", scene )
-- "exitScene" event is dispatched before next scene's transition begins
scene:addEventListener( "exitScene", scene )
-- "destroyScene" event is dispatched before view is unloaded, which can be
-- automatically unloaded in low memory situations, or explicitly via a call to
-- storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( "destroyScene", scene )
---------------------------------------------------------------------------------
return scene
Scene2.lua
[code]
–
– scenetemplate.lua
local storyboard = require( “storyboard” )
local widget = require “widget”
local scene = storyboard.newScene()
local bgImage, playButton
local function gotoScene1()
storyboard.gotoScene( “Scene1”, “crossFade”, 800 )
end
– BEGINNING OF YOUR IMPLEMENTATION
– Called when the scene’s view does not exist:
function scene:createScene( event )
local group = self.view
bgImage = display.newImageRect( “Home-Page-BG.png”, 320, 568 )
bgImage:setReferencePoint( display.CenterReferencePoint )
bgImage.x = 160
bgImage.y = 200
group:insert( bgImage )
playButton = widget.newButton{
default = “Play-Button.png”,
over = “Play-Button.png”,
onPress = gotoScene1,
onEvent = buttonHandler,
id = “playButton”,
width = 198,
height = 69.5
}
playButton.x = 160; playButton.y = 400
group:insert( playButton )
end
– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
storyboard.removeScene( “Scene1” )
– INSERT code here (e.g. start timers, load audio, start listeners, etc.)
end
– Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view
– INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.)
end
– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )
local group = self.view
– INSERT code here (e.g. remove listeners, widgets, save state, etc.)
end
– END OF YOUR IMPLEMENTATION
– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )
– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )
– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )
– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )
return scene
[/code] [import]uid: 107915 topic_id: 34468 reply_id: 137202[/import]