Insert "display group" to "scene group" problem

I use storyboard.

In one scene there is a scene group:

group = self.view

And an another display group:

pathGroup = display.newGroup()

I insert the pathGroup into scene group:

group:insert(pathGroup)

Everything works fine first time, but when I enter the scene again , I’ll get an error in every sentence like this:

pathGroup:insert(nuolianimaatio)

Error message is following:

attempt to call method 'insert' (a nil value)

In the exitScene function I nil the groups:

        for i=pathGroup.numChildren,1,-1 do             local child = pathGroup[i]             child.parent:remove( pathGroup )             child=nil               end         for i=group.numChildren,1,-1 do             local child = group[i]             child.parent:remove( group )             child=nil         end

I’m probably missing something. Thanks in advance!

Where are you creating the pathGroup?  Is it in createScene()?  CreateScene doesn’t necessarily run everytime a scene is entered.

It is at “top of the code”. Not in scene functions. Don’t know if that is a proper way to do it, but that way it’s visible to all scene functions and I use it in CreateScene, EnterScene and ExitScene.

Things not in a scene function only get loaded on the initial “require” that Storyboard does when it goes to the scene the very first time.  When you revisit the scene if you’ve done something to kill that group, it won’t get re-created again. 

We tried to address these issues in this blog post:

http://www.coronalabs.com/blog/2013/08/20/tutorial-reloading-storyboard-scenes/

While it’s about reloading scenes, many things apply to revisting scenes.

Thanks! The video was really good. Funny thing is that before I had this problem I removed the scene that I just visited in the following scene, and everything worked fine. Now I know why :wink: Then I took the removeScenes away, 'cause I thought it would be better because scenes are revisited often, and got the error.

The game structure is basicly this:

  1. menu
  2. map
  3. game
  4. map
  5. game
  6. map
  7. game
  8. and so on…

One extra question. So it is not necessary to nil every display object from scene’s display group, sounds etc, if I’m going to revisit the scene?

Only display objects go into the scene’s group, so you still have to dispose of sounds, timers, etc.

Thanks Rob again! I really appreciate this :slight_smile:

Now in every exitScene I nil the transitions and scene groups display objects and this is unnecessary if the scene is revisited after a while? Here is the code of exitScene function:

for i=group.numChildren,1,-1 do local child = group[i] child.parent:remove( group ) child=nil end for i=1,#transitiot do         transition.cancel( transitiot[i] )         transitiot[i]=nil end

I also cancel timers and remove sounds. But the question is about the display objects and the transitions.

And is there an article that I could read more about this?

Transitions should be cancelled and nil’ed they are not display objects.  They are not things that get added to the group.

Where are you creating the pathGroup?  Is it in createScene()?  CreateScene doesn’t necessarily run everytime a scene is entered.

It is at “top of the code”. Not in scene functions. Don’t know if that is a proper way to do it, but that way it’s visible to all scene functions and I use it in CreateScene, EnterScene and ExitScene.

Things not in a scene function only get loaded on the initial “require” that Storyboard does when it goes to the scene the very first time.  When you revisit the scene if you’ve done something to kill that group, it won’t get re-created again. 

We tried to address these issues in this blog post:

http://www.coronalabs.com/blog/2013/08/20/tutorial-reloading-storyboard-scenes/

While it’s about reloading scenes, many things apply to revisting scenes.

Thanks! The video was really good. Funny thing is that before I had this problem I removed the scene that I just visited in the following scene, and everything worked fine. Now I know why :wink: Then I took the removeScenes away, 'cause I thought it would be better because scenes are revisited often, and got the error.

The game structure is basicly this:

  1. menu
  2. map
  3. game
  4. map
  5. game
  6. map
  7. game
  8. and so on…

One extra question. So it is not necessary to nil every display object from scene’s display group, sounds etc, if I’m going to revisit the scene?

Only display objects go into the scene’s group, so you still have to dispose of sounds, timers, etc.

Thanks Rob again! I really appreciate this :slight_smile:

Now in every exitScene I nil the transitions and scene groups display objects and this is unnecessary if the scene is revisited after a while? Here is the code of exitScene function:

for i=group.numChildren,1,-1 do local child = group[i] child.parent:remove( group ) child=nil end for i=1,#transitiot do         transition.cancel( transitiot[i] )         transitiot[i]=nil end

I also cancel timers and remove sounds. But the question is about the display objects and the transitions.

And is there an article that I could read more about this?

Transitions should be cancelled and nil’ed they are not display objects.  They are not things that get added to the group.