Proper way to switch scenes, remove objects and groups.

Hey all,

I’m pretty new to corona, and while I’ve made great headway into my game, with a great menu and start of first level, I’ve run into a problem I’m having trouble with regarding scene changes using the storyboard API,

I’m unsure of what I’m doing wrong but I have a problem where first my menu was bleeding into my first level, then I managed to get that to stop by using “removeSelf()”

I add these two layers like so

 backgroundItems = self.view  
 menu = display.newGroup();  

in my menu I exit like this currently :

function scene:exitScene( event ) print("Exit:scene:exitScene") menu:removeSelf() end

if I remove the menu:removeSelf() it’ll appear in my level. if I leave it, if I return to my menu the menu is gone… backgrounditems group comes and goes as it should though,

All works like it should if I use

menu = self.view backgroundItems = self.view

but then I can’t slide my menu around as I’d like, the whole scene moves…

I figure it’s because I’ve added it as a group, because of the way my menu works, 1 layer basically floating above another layer, and onpress the menu slides back and forth to different areas

so basically I’m looking for the proper way to add groups, and remove them between scenes… because clearly I’m missing something

Any help in the right direction here would be appreciated, I’m likely doing something pretty stupid but as I said, new to lua and corona :wink:

[import]uid: 157863 topic_id: 30254 reply_id: 330254[/import]

You need to add all of your display objects to the scene’s view and treat that as the parent display group. For example:

[lua]backgroundItems = self.view
menu = display.newGroup()
backgroundItems:insert( menu )[/lua] [import]uid: 8271 topic_id: 30254 reply_id: 121222[/import]

  
local backgroundItems = display.newGroup();  
local menu = display.newGroup();  
function scene:createScene( event )  
 local group = self.view  
  
 -- create BG and Menu code and insert in group  
 -- PLACEHOLDER  
  
  
 group:insert(backgroundItems)  
 group:insert(menu)  
end  
  
function scene:exitScene( event )  
 -- you don't need this anymore  
end  

[import]uid: 98393 topic_id: 30254 reply_id: 121223[/import]

Sorry Paul, but I disagree. You should not be creating your display objects when the module loads because they will appear as part of the stage, not as part of the scene until your create scene is called. This means that for the time between loading the scene and putting it into view, those display objects are basically zombies. As per the corona tutorials (see the blog, especially recent entries) you should be creating your display objects in the create scene function. You can always load and defer the goto until you want to show it. [import]uid: 8271 topic_id: 30254 reply_id: 121231[/import]

hi, do you mean i should put

local backgroundItems = display.newGroup();  
local menu = display.newGroup();  

in create scene ?
example

[code]

local backgroundItems – i still need access to it outside of createscene
local menu

function scene:createScene( event )
local group = self.view

backgroundItems = display.newGroup();
menu = display.newGroup();

– example
item1 = display.newImageRect( “item.png”, 100, 100 )
backgroundItems:insert(item1)

btn = display.newImageRect(“btn.png”, 10, 10)
menu:insert(btn)

group:insert(backgroundItems)
group:insert(menu)
end
[/code] [import]uid: 98393 topic_id: 30254 reply_id: 121234[/import]

Or perhaps more like this:

local backgroundItems  
local menu  
   
function scene:createScene( event )  
 local group = self.view  
  
 backgroundItems = display.newGroup();  
 menu = display.newGroup();  
 -- create BG and Menu code and insert in group  
 -- PLACEHOLDER  
  
  
 group:insert(backgroundItems)  
 group:insert(menu)  
end  
   
function scene:exitScene( event )  
 -- you don't need this anymore  
end  

If it displays and you want it to not display when the scene changes, add it in createScene and make sure it ends up in “group”… either group:insert(myObject) or if you need additional grouping like the example above, the groups get put into “group”.

exitScene() is where you remove things that are not in groups, like native.textFields, timers, runtime event listeners, stopping transitions, etc.
[import]uid: 19626 topic_id: 30254 reply_id: 121238[/import]

I sorted this out last night :slight_smile: the problem was I wasn’t sticking my “group” into my main view as a child
so basically did as rob posted above

local group, menu   
   
function scene:createScene( event )  
 group = self.view  
 menu = display.newGroup();  
  
- created all my stuff here   
and added my functional code to "enterScene"   
  
  
 group:insert(backgroundItems)  
 group:insert(menu)  
end  
   
function scene:exitScene( event )  
 -- removed my timers and stuff here   
end  

I think my problem was over excitement
[import]uid: 157863 topic_id: 30254 reply_id: 121273[/import]

Yes, that’s as we both were trying to say, I think.

Perhaps the most telling difference between our suggestions is that I prefer not to use module level local variables, but it’s not wrong, just different. [import]uid: 8271 topic_id: 30254 reply_id: 121318[/import]

You need to add all of your display objects to the scene’s view and treat that as the parent display group. For example:

[lua]backgroundItems = self.view
menu = display.newGroup()
backgroundItems:insert( menu )[/lua] [import]uid: 8271 topic_id: 30254 reply_id: 121222[/import]

  
local backgroundItems = display.newGroup();  
local menu = display.newGroup();  
function scene:createScene( event )  
 local group = self.view  
  
 -- create BG and Menu code and insert in group  
 -- PLACEHOLDER  
  
  
 group:insert(backgroundItems)  
 group:insert(menu)  
end  
  
function scene:exitScene( event )  
 -- you don't need this anymore  
end  

[import]uid: 98393 topic_id: 30254 reply_id: 121223[/import]

Sorry Paul, but I disagree. You should not be creating your display objects when the module loads because they will appear as part of the stage, not as part of the scene until your create scene is called. This means that for the time between loading the scene and putting it into view, those display objects are basically zombies. As per the corona tutorials (see the blog, especially recent entries) you should be creating your display objects in the create scene function. You can always load and defer the goto until you want to show it. [import]uid: 8271 topic_id: 30254 reply_id: 121231[/import]

hi, do you mean i should put

local backgroundItems = display.newGroup();  
local menu = display.newGroup();  

in create scene ?
example

[code]

local backgroundItems – i still need access to it outside of createscene
local menu

function scene:createScene( event )
local group = self.view

backgroundItems = display.newGroup();
menu = display.newGroup();

– example
item1 = display.newImageRect( “item.png”, 100, 100 )
backgroundItems:insert(item1)

btn = display.newImageRect(“btn.png”, 10, 10)
menu:insert(btn)

group:insert(backgroundItems)
group:insert(menu)
end
[/code] [import]uid: 98393 topic_id: 30254 reply_id: 121234[/import]

Or perhaps more like this:

local backgroundItems  
local menu  
   
function scene:createScene( event )  
 local group = self.view  
  
 backgroundItems = display.newGroup();  
 menu = display.newGroup();  
 -- create BG and Menu code and insert in group  
 -- PLACEHOLDER  
  
  
 group:insert(backgroundItems)  
 group:insert(menu)  
end  
   
function scene:exitScene( event )  
 -- you don't need this anymore  
end  

If it displays and you want it to not display when the scene changes, add it in createScene and make sure it ends up in “group”… either group:insert(myObject) or if you need additional grouping like the example above, the groups get put into “group”.

exitScene() is where you remove things that are not in groups, like native.textFields, timers, runtime event listeners, stopping transitions, etc.
[import]uid: 19626 topic_id: 30254 reply_id: 121238[/import]

I sorted this out last night :slight_smile: the problem was I wasn’t sticking my “group” into my main view as a child
so basically did as rob posted above

local group, menu   
   
function scene:createScene( event )  
 group = self.view  
 menu = display.newGroup();  
  
- created all my stuff here   
and added my functional code to "enterScene"   
  
  
 group:insert(backgroundItems)  
 group:insert(menu)  
end  
   
function scene:exitScene( event )  
 -- removed my timers and stuff here   
end  

I think my problem was over excitement
[import]uid: 157863 topic_id: 30254 reply_id: 121273[/import]

Yes, that’s as we both were trying to say, I think.

Perhaps the most telling difference between our suggestions is that I prefer not to use module level local variables, but it’s not wrong, just different. [import]uid: 8271 topic_id: 30254 reply_id: 121318[/import]