insert into SceneGroup Self.view outside of the createScene function

Ive been using storyboard as my method for scene management and am having issues inserting my display objects (non background and UI objects) which are created from spawn functions and game loops etc. 

At the moment I use display.newGroups to store the new objects but would like to somehow include everything inside the sceneGroup so that Corona SDK can handle clearing the scenes rather. I have also noticed that if objects are not inside the sceneGroup, the storyboard.gotoScene transitions don’t work on anything other than the sceneGroup.

So yeah, Is there a way I could insert objects from spawn functions etc… into the screenGroup self.view inside the createScene ?

(I know storyboard doesn’t actually use sceneGroups, but I’m hoping you know what I mean)

Thanks

This is quite very possible. Let me explain things first. In both Composer and Storyboard (it’s older brother), there is a “scene” object. Part of that scene object is a display.newGroup called view, i.e. scene.view. This is the actual group you are using.

Inside scene:createScene() there is a line:

local sceneGroup = self.view

self is a reference to scene since this method is attached to scene via a : operator. There is an invisible variable “self” that is passed to createScene for you. Therefore self.view = scene.view. All the above line does in the end is give you a local reference to scene.view (via self.view).

You can always add your functions outside of scene:createScene() (and this works for Composer too) to the scene object:

function scene:spawnEnemy()     local sceneGroup = self.view ... end

When spawnEnemy runs, it will get “self” passed to it since it’s part of scene group. However most people just have local functions sitting in the main chunk which does not have the free reference to the scene object. But in this case it’s still uber simple to do:

local function spawnEnemy()     local sceneGroup = scene.view ... end

Remember scene.view is the actual group you’re trying to use.

Rob

Ahhh I see now. Thanks Rob for that detailed explanation. I now understand exactly how they work. Thanks again!

Hi Rob,

so when I do what you just showed :

local function spawnEnemy() local sceneGroup = scene.view ... end 

and I add all my display objects created with spawnEnemy() into the local sceneGroup, are they automatically housekept too when the whole scene is removed  (eg. when scene:destroy is called)  ?  

I always had this question too and i’m glad dip has raised this.

Cheers. 

Yes. For Composer to manage your objects they must be inserted into the group scene.view. For functions are are added as methods to the scene, i.e.

function scene:create

function scene:show

function scene:doSomethingAwesome

and are using the colon (:slight_smile: operator, those functions get “self” passed to them. In this context self = scene. So if you’re functions are part of the scene object, you can use self.view or scene.view interchangeably. If you are using just normal local functions:

local function doSomethingIncredible

These don’t know about self, but as long as they are defined after creating the scene:

local scene = composer.newScene()

then you can use scene.view to accomplish the same thing self.view does in the object functions.

awesome!  now i feel more confident about housekeeping!  

thanks a lot!  

This is quite very possible. Let me explain things first. In both Composer and Storyboard (it’s older brother), there is a “scene” object. Part of that scene object is a display.newGroup called view, i.e. scene.view. This is the actual group you are using.

Inside scene:createScene() there is a line:

local sceneGroup = self.view

self is a reference to scene since this method is attached to scene via a : operator. There is an invisible variable “self” that is passed to createScene for you. Therefore self.view = scene.view. All the above line does in the end is give you a local reference to scene.view (via self.view).

You can always add your functions outside of scene:createScene() (and this works for Composer too) to the scene object:

function scene:spawnEnemy()     local sceneGroup = self.view ... end

When spawnEnemy runs, it will get “self” passed to it since it’s part of scene group. However most people just have local functions sitting in the main chunk which does not have the free reference to the scene object. But in this case it’s still uber simple to do:

local function spawnEnemy()     local sceneGroup = scene.view ... end

Remember scene.view is the actual group you’re trying to use.

Rob

Ahhh I see now. Thanks Rob for that detailed explanation. I now understand exactly how they work. Thanks again!

Hi Rob,

so when I do what you just showed :

local function spawnEnemy() local sceneGroup = scene.view ... end 

and I add all my display objects created with spawnEnemy() into the local sceneGroup, are they automatically housekept too when the whole scene is removed  (eg. when scene:destroy is called)  ?  

I always had this question too and i’m glad dip has raised this.

Cheers. 

Yes. For Composer to manage your objects they must be inserted into the group scene.view. For functions are are added as methods to the scene, i.e.

function scene:create

function scene:show

function scene:doSomethingAwesome

and are using the colon (:slight_smile: operator, those functions get “self” passed to them. In this context self = scene. So if you’re functions are part of the scene object, you can use self.view or scene.view interchangeably. If you are using just normal local functions:

local function doSomethingIncredible

These don’t know about self, but as long as they are defined after creating the scene:

local scene = composer.newScene()

then you can use scene.view to accomplish the same thing self.view does in the object functions.

awesome!  now i feel more confident about housekeeping!  

thanks a lot!