Example code for Stages?

Hi,

In the Programming Guide, there was a section on the best way to manage separate screens, like the Main Options screen, gameplay screen etc using groups.

Could you please post an example using Corona Best Practices? I’m sure I could hack one up pretty easily, but there must be a best way to optimize for memory, turn off animations etc etc.

Also, could we get a description of an optimized workflow? What editors are people using right now? Is Corona being used directly with any IDEs?

Thanks!
ps, one last thing. I’m a little worried about my memory management, and being certain to nil things at the right time. I’m new to Lua, so I dont know if there is a way to list all variable and object easily. I would like to be able to list them now and then, to make sure I’m not accumulating junk. [import]uid: 5652 topic_id: 1265 reply_id: 301265[/import]

I’ve been using display groups inside separate lua modules as a way of organizing screens. For example, you want to have an OptionsScreen.lua file that has functions to manipulate the screen, such as OptionsScreen:show() and OptionsScreen:hide(). Also, inside that module, you add your screen elements to a group so that in your hide() function you can turn that group invisible instead of trying to hide or nil all the graphic elements individually within that screen. Or, if you don’t need that screen anymore, discard the elements in that function and recreate them on the in the show() function.

Personally, I have a over all DisplayController object that manages all of the screen modules and I interact with that to tell it which screen I want in the foreground. That in itself has a display group, which is a hierarchy of screens and items to show. So, I might make a call to DisplayController:showOptionsScreen() which in turn will call the OptionsScreen:show() function. This is nice since I can use DisplayController to do things before and after I show the OptionsScreen, such hide the previous screen.

The hierarchy would look something like:

GameController
|-> DisplayController
|–> (OptionsScreen, HomeScreen, AboutScreen etc)

As for IDEs, I still use XCode. I’m familiar with it, it allows me to organize files into groups/folders and it does the syntax highlighting and rudimentary code completion with a plugin.

As for memory management, the first rule of optimization is: don’t do it. Lua has garbage collection, so for the most part, you don’t need to worry about it.

I hope that helps.
-Mike
[import]uid: 7144 topic_id: 1265 reply_id: 3450[/import]

Thanks for the great reply!

My first games is coming along nicely, but now thats its around a thousand lines of code, my lack of experience is really starting to show. I’m having a lot of trouble managing screens still, and I’m afraid I will have to do some serious refactoring to get my code into into something I can squeeze into your GameController -> DisplayController -> OptionsScreen model.

But that must be better system than what I have now, which is chaos :slight_smile:

Thanks again!

PS. if you have an empty project using your Controller setup, I would really appreciate being able to take a look at the files! Your name will also go straight into the Credits section of my game :slight_smile: [import]uid: 5652 topic_id: 1265 reply_id: 3481[/import]

It’ll come with experience of having built a few and you’ll soon grasp it. Also, object oriented programming is your friend and in no other situation does it makes sense to build real life objects in code, than with a game. It’s also a great way, if not the best way, to organize your code into manageable pieces.

I’ll throw together a skeleton application tomorrow to show you and anyone else that’s interested how I structure my games. I usually start with a skeleton app anyway with empty modules of the items I think I might want in my game. Then, feature by feature, I add it slowly, play the game, tweak it, add another feature, play, tweak, continue. For example, I’ll create a GunController that handles the creation of Bullet objects, and their firing, but instead of implementing it all at once, I might have the GunController:fireBullet() function simply print out “bang” just so I know it works. Then when I’m ready, I can start filling out that function, creating a Bullet instance, setting it’s trajectory and other useful info, and then animating it on it’s way toward the enemy (which of course will be completed once I get bullets firing). It’s all about the baby steps.
-Mike
[import]uid: 7144 topic_id: 1265 reply_id: 3482[/import]

Thanks, much appreciated!

My game logic is finished, so over the weekend I tried setting up the code as you suggested, but its not working so well for me. Separating out code-sections into individual files with the “module(…, package.seeall)” trick is playing havoc with my scoping; I’m having to make lots of things globals just to get it all running.

This menu and screens stuff is far trickier than writing a simple puzzle game in my opinion, but I’m hoping that this trouble its a one-time thing; once I know how its supposed to fit together, the next time it will be trivial :slight_smile:

It certainly is a different way to think than with the procedural coding I do for scientific scripting.

-Dave [import]uid: 5652 topic_id: 1265 reply_id: 3519[/import]

I’m not an expert in Lua, so it took me a while to figure out how to get modules to work at all and my code may not be the best way to do it but it works. Also, I only have one main DisplayController in this template, but you should have more as your code grows. For example, you may want an OpponentsDisplayController that handles drawing your opponents on the screen. I put the code in a post so others could find it and get to it as well:

http://www.mikeziray.com/2010/06/27/corona-game-template-in-lua/

If anyone sees something that I could do better, I’m learning along with everyone else, so please let me know how I can improve my Lua code.
Thanks!
Mike
[import]uid: 7144 topic_id: 1265 reply_id: 3520[/import]

Mike,

I tried to download the file but the zip file is empty 0 bytes.
Can you please look at it?

Thanks,
Danny [import]uid: 6873 topic_id: 1265 reply_id: 3524[/import]

Thanks for letting me know. Not sure what happened, but I re-uploaded it and it should work fine now.
-Mike
[import]uid: 7144 topic_id: 1265 reply_id: 3526[/import]

I learned how to use most of the codes in corona but i cant figure out how to manage screens for different game levels. Does anyone know if the have sample apps that I can look at? [import]uid: 31928 topic_id: 1265 reply_id: 22735[/import]