Switching from Director to Storyboard API

Hello,

I am planning to switch all Kwik projects from Director class to the new Storyboard API. Unfortunately I couldn't have time to go deep into the new API and I was wondering if someone else have already made this transition, and is willing to share a few tips.

Here how Kwik works currently:
Main.lua

1
2
3
4
5
6
7
8
9
10
11
12
local director = require("director") 
-- Create a main group
local mainGroup = display.newGroup()
local initPage = 1
 
-- Main function
local function main()
        mainGroup:insert(director.directorView) 
        director:changeScene( "page_"..initPage )
        return true
end
main()

Questions are:
- do I need to create all scene:functions for each page?

For your menu page, you probably need to create them, but you don't have to do much if any code for them.

scene:createScene

is where you're going to do most of your work. Load your background, create your buttons, etc.

scene:enterScene

gets called after the scene is created and here is a place to start animations, assign Runtime:("enterFrame", *) event listeners, start timers, etc.

scene:exitScene

would be where you undo all the timers, listeners and such that you started in enterScene

scene:destroyScene

would hold code you would typically put into a director clean() function. In other words destroy objects not in the display group that is returned from createScene.

So createScene is the same as director's new(), destroyScene is the same as director's clean() and you have two extra points to deal with stuff that should happen after things are loaded and need to end before switching screens.

One difference with storyboard is that it does keep old scenes around until it needs to free up memory and thus destroyScene gets called at some point in the future when we need memory back. exitScene is there because you don't want your timers/transitions and such to keep running after you switch scenes and if you come back to a scene, it should be in the same state you left it, so enter Scene is to start those items up again.

Once you get used to it, its a natural "Corona SDK" way of doing things.

- what is the best way to keep all scene elements under a single group (like the menuGroup above)?

storyboard creates a group called "group". This is the same as the typical director "localGroup" which you seem to have renamed to "menuGroup".

- do I really need to add the require("storyboard") in all pages?
It appears so. But you could load storyboard in your main.lua once and leave off the local, but then it would reside in global space, not local space and as we know, globals are to be avoided when possible.

- does anyone has a better or cleaner way to transition from Director to Storyboard?
I'm not transitioning. I started a new project with Storyboard and its going pretty good. I've not tried to convert a director project to storyboard yet.

But as I mentioned above: new becomes scene:createScene, clean becomes scene:destroyScene, rename localGroup to group. You don't return group anymore. The code returns a scene object. I wouldn't think it would be that hard, but I've not tried it.

Storyboard DOES NOT support popup's yet, but since your scene isn't destroyed when you switch away from it, I might be able to set a variable "returnTo" that my popup scenes know to go back to when its said and done. Still working out that little bit.

Thanks a lot Rob. I will give a try.

I though Director was cleaning pages automatically when switching them. I have never used the Director's clean() function.

Rob, that was very helpful to me, too. But I have one question.

Do I have to add all of my display objects to the "group"?

Yes, and no.

Any objects you want to go away when the scene switches away, needs to be in group. But lets say your building a game with some types of heads of display, inventory box, stuff you want on every screen, no you would not put those into group.

So its the same as with Director. If you would put it in "localGroup" you would put it into "group" with storyboard.

views:1490 update:2011/11/27 10:14:33
corona forums © 2003-2011