storyboard - why could I not put all setup in "createScene"?

I’m trying to better understand storyBoard. Have read the blog posts. One question that would assist would be:

Question 1 - Why could I not put all setup for a scene in the “createScene” callback? That is what would be the con’s (negative effects) of doing this and why? (e.g. including setup of listeners)

As an example I would like to call out from a scene to a separate class (build in an OO type approach) and just call it’s setup method, which would display it on the screen, set up it’s listeners etc. However currently it would seem with story board I would really need to program into my item’s class a “createSceneSetup” and “enterSceneSetup”. For example:

In the scene (where I call my class/object):

function scene:createScene( event )  
 myobject:createSceneSetup()  
end  
  
function scene:enterScene( event )  
 myobject:enterSceneSetup()  
end  

Question 2 - Overall then, when encaptulating code into my own classes/objects, do you recommend that I do stick with separate methods on them for createSceneSetup & enterSceneSetup? [import]uid: 140210 topic_id: 25399 reply_id: 325399[/import]

Hello,

Those are very valid questions, I’ll try to answer them to the best of my abilities.

Answer to Question 1 —

The reason why you should only put object-creation code in your “createScene” event is because whenever you call storyboard.gotoScene(), if the scene’s view already exists (e.g. the scene has been loaded previously), then the “createScene” event will not be dispatched again (enterScene events, however, are dispatched after every scene transition). So if you were to put important logic code in your “createScene” event and the scene is already “created” so the event doesn’t get dispatched, that can obviously cause problems with your app (because some of your logic will be missing in that scenario).

Of course, if you were to purge every scene after moving to a new scene, then you *could* put all your logic in the “createScene” event, but it’s not recommended.

You’ll find that as your project becomes larger and larger, you’ll appreciate the separation between object-creation code, and game/app “logic” code.

Answer to Question 2 —

I’m not really sure what you’re asking here, but as a rule of thumb, if code can be re-used elsewhere, it’s best to structure your methods so that they can be flexible enough for several different functions to take advantage of them (and have minor changes via arguments). [import]uid: 52430 topic_id: 25399 reply_id: 103418[/import]

thanks jonathan - to clarify re Question 1

Is there a reason why the game would not work properly if you setup your listeners in the “createScene” area?

Like, if it was a listener on an object in Scene1, then if you went to Scene 2 then you wouldn’t be able to click on the object in that scene anyway. So my question (just for understanding sake) is would the object/listener/function connections fail if all were setup in “createScene”?
[import]uid: 140210 topic_id: 25399 reply_id: 103420[/import]

@greg886: If you were to put your object creation code in the “createScene” event, then it would be fine if you put your listeners for those same objects within the “createScene” listener; however, I recommend just putting them above all the scene events.

I consider things like object listeners, single-task functions, etc. “private functions” and I usually place all those above my scene event listeners, so all listeners have access to them and they are all in a nice little area in the module. [import]uid: 52430 topic_id: 25399 reply_id: 103421[/import]