What is the correct way of inserting display object into storyboard?

I have been using storyboard for quite some time now and i am confused what is the best method to insert objects into the scenes, which one is the correct way, if there is no correct way between the two, is there a better way to do it?

[lua]local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
local obj
function scene:createScene( event )
local group = self.view
obj = display.newCircle(100,100,100)
group:insert(obj)
end
function scene:enterScene( event )
local group = self.view
obj.x, obj.y = 200, 200
end
function scene:exitScene( event )
local group = self.view
obj:removeSelf()
obj = nil
end
scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene )
return scene[/lua]

[lua]local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
function scene:createScene( event )
local group = self.view
local obj = display.newCircle(100,100,100)
group:insert(obj)
end
function scene:enterScene( event )
local group = self.view
local obj = group[1]
obj.x, obj.y = 200, 200
end
function scene:exitScene( event )
local group = self.view
local obj = group[1]
obj:removeSelf()
obj = nil
end
scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene )
return scene[/lua] [import]uid: 114118 topic_id: 32690 reply_id: 332690[/import]

I use your first method but I don’t remove it in exitScene, presumed anything in the StoryBoard group, Corona handles.

In fact I don’t think you should destroy it in exitScene, if you come back to that scene and the scene has not been destroyed, the circle wouldn’t be created again.

If the object isn’t going to be handled in any other function (like buttons etc…) then I make it local in CreateScene and also position it in there as well.

Dave [import]uid: 117617 topic_id: 32690 reply_id: 129971[/import]

I use your first method but I don’t remove it in exitScene, presumed anything in the StoryBoard group, Corona handles.

In fact I don’t think you should destroy it in exitScene, if you come back to that scene and the scene has not been destroyed, the circle wouldn’t be created again.

If the object isn’t going to be handled in any other function (like buttons etc…) then I make it local in CreateScene and also position it in there as well.

Dave [import]uid: 117617 topic_id: 32690 reply_id: 129971[/import]

Anything you put into “group”, storyboard will manage removing for you as @thedavebaxter pointed out…

I think the bigger question your asking here is do you make your objects local in the functions or outside of it.

In #1, you declare your object at the top and then create it in the scene. In #2, you create a variable that is local only to createScene, then jump through extra hoops to get access to again. Clearly #1 is the cleaner way and how I do things… Sort of.

When you are creating scenes, there are things you need access to outside of the createScene function and things you don’t need access to. For instance, are you ever referencing your background again after you create it? Do you access your buttons in enterScene or functions outside of the storyboard functions? If you don’t reference them outside of createScene, you should go ahead and make them local there. But if you touch it elsewhere, define it at the top.

For instance:

local storyboard = require( "storyboard" )  
local scene = storyboard.newScene()  
local obj  
function scene:createScene( event )  
 local group = self.view  
  
 local background = display.newRect(0,0,display.contentWidth,display.contentHeight)  
 -- never going to access this again, so it can be local  
  
 local button = display.newRect(100,300,100,50)  
 group:insert(button)  
 local function touchButton(event)  
 if event.phase == "ended" then  
 -- do stuff  
 end  
 return true  
 end  
 button:addEventListener("touch",touchButton)  
 --  
 -- since button's handler is also local to createScene, we don't need to have it scoped   
 -- outside....  
  
 obj = display.newCircle(100,100,100)  
 group:insert(obj)  
end  
function scene:enterScene( event )  
 local group = self.view  
 obj.x, obj.y = 200, 200 -- this executes after the scene is on the screen, which can   
 -- cause your viewer to see it at 100,100 then jump to 200,200.  
 -- this needs to be defined at the top of the scene because we are referencing it here, as well as  
 -- createScene.  
end  
function scene:exitScene( event )  
 local group = self.view  
 -- the only thing you need to remove here is things created in enterScene or timers/runtime listeners.  
end  
scene:addEventListener( "createScene", scene )  
scene:addEventListener( "enterScene", scene )  
scene:addEventListener( "exitScene", scene )  
return scene  

Hopefully the comments above will help!
[import]uid: 19626 topic_id: 32690 reply_id: 129998[/import]

Anything you put into “group”, storyboard will manage removing for you as @thedavebaxter pointed out…

I think the bigger question your asking here is do you make your objects local in the functions or outside of it.

In #1, you declare your object at the top and then create it in the scene. In #2, you create a variable that is local only to createScene, then jump through extra hoops to get access to again. Clearly #1 is the cleaner way and how I do things… Sort of.

When you are creating scenes, there are things you need access to outside of the createScene function and things you don’t need access to. For instance, are you ever referencing your background again after you create it? Do you access your buttons in enterScene or functions outside of the storyboard functions? If you don’t reference them outside of createScene, you should go ahead and make them local there. But if you touch it elsewhere, define it at the top.

For instance:

local storyboard = require( "storyboard" )  
local scene = storyboard.newScene()  
local obj  
function scene:createScene( event )  
 local group = self.view  
  
 local background = display.newRect(0,0,display.contentWidth,display.contentHeight)  
 -- never going to access this again, so it can be local  
  
 local button = display.newRect(100,300,100,50)  
 group:insert(button)  
 local function touchButton(event)  
 if event.phase == "ended" then  
 -- do stuff  
 end  
 return true  
 end  
 button:addEventListener("touch",touchButton)  
 --  
 -- since button's handler is also local to createScene, we don't need to have it scoped   
 -- outside....  
  
 obj = display.newCircle(100,100,100)  
 group:insert(obj)  
end  
function scene:enterScene( event )  
 local group = self.view  
 obj.x, obj.y = 200, 200 -- this executes after the scene is on the screen, which can   
 -- cause your viewer to see it at 100,100 then jump to 200,200.  
 -- this needs to be defined at the top of the scene because we are referencing it here, as well as  
 -- createScene.  
end  
function scene:exitScene( event )  
 local group = self.view  
 -- the only thing you need to remove here is things created in enterScene or timers/runtime listeners.  
end  
scene:addEventListener( "createScene", scene )  
scene:addEventListener( "enterScene", scene )  
scene:addEventListener( "exitScene", scene )  
return scene  

Hopefully the comments above will help!
[import]uid: 19626 topic_id: 32690 reply_id: 129998[/import]

Thanks for the reply guys, the code i pasted was a highly simplified example, i don’t really remove display objects on exitscene nor do i move objects on enterscene, what i gathered from your answers is that anything that needs to be referenced in more then one function should be initialized outside and everything else needs to be local inside the function, correct me if i’m wrong [import]uid: 114118 topic_id: 32690 reply_id: 130123[/import]

Thanks for the reply guys, the code i pasted was a highly simplified example, i don’t really remove display objects on exitscene nor do i move objects on enterscene, what i gathered from your answers is that anything that needs to be referenced in more then one function should be initialized outside and everything else needs to be local inside the function, correct me if i’m wrong [import]uid: 114118 topic_id: 32690 reply_id: 130123[/import]