Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Best way to structure code / main loop
Started by garysims Jan 15 2010 02:06 AM

- - - - -
3 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

garysims

[GLOBAL: userInfoPane.html]
garysims
  • Enthusiast

  • 54 posts
  • Corona SDK

Hi,

I'm new to lua and corona and I am getting my head around some of the ideas/principles... I have a simple game which I am writing and the code looks something like this:

--------------------------
function dolevel(level)
-- Create display objects and layout
...
end

-- Accelerometer function
local function accel( event )
-- Read the Accelerometer and more the objects / sprites accordingly
-- Also does collision detection
...
end

local function frameupdate()
-- Do the animation
-- Call object:translate as needed to move the sprites / objects
end

local function main()
dolevel(1)
system.setAccelerometerInterval(10)
Runtime:addEventListener( "accelerometer", accel )
Runtime:addEventListener( "enterFrame", frameupdate );
end

main()
--------------------------

Now my question is this... In the accelerometer or the enterFrame listeners I can detect if the level has been completed. My question what is the best way to commnicate that with main() so that the 2 EventListeners can be removed, the next level created and the EventListeners added again.

Should I loop in main()? I am guessing that will lots of CPU...

This is more about structure than actual code... Any ideas????

Thanks,

Gary
uid: 4415 topic_id: 393 reply_id: 300393


[TOPIC: post.html]
#2

fpassa

[GLOBAL: userInfoPane.html]
fpassa
  • Contributor

  • 151 posts
  • Corona SDK

You can use a global table to switch between levels.

For instance if you take a look at my game, Ultimate Coins Game, also here in Customer, you will see what you are looking for.

Basically I have a SwitchLevel function, which erase any point, score, and level stuff. Before prepare the level, it calls a function called freeStage() which clear all children of level table, which allow me to reinsert new objects on it. Then a simple transition.dissolve or directly background:insert(level) or even level.isVisible=true, allow me to refresh level previous screen.

In my game, main loop is controlled by a 1 sec call to a timer event, which handle level time. Of course each coins on the screen has its own touch event listener. In addition there is a runtime accelerometer event to detect Shake.

In general the idea is to have separeted function to setup something, delete something, update something (in fact the function that really refresh the screen) and control events (like object or runtime listeners and/or timer events).

If you are updating animation by using framerate, you must make some calculation to prevent different timing refresh. For better controll of animation I prefer to use timer, which guarranty equal animation on all devices.

I can't provide you my source code of Ultimate Coins Game, but I can give you some directions based on my experience.

May be in a near future I will make some tutorial if there is interest on that.

Regards,

Flavio (VLS)
vlsarg.blogspot.com
uid: 3022 topic_id: 393 reply_id: 729


[TOPIC: post.html]
#3

evank

[GLOBAL: userInfoPane.html]
evank
  • Contributor

  • 317 posts
  • Alumni

Hi, garysims:

If I understand your question, I think the first thing you should probably do is get all your animation updates into one master function that gets called on the enterFrame event. Alternatively, you could call this update on a timer, as fpassa suggests -- but since the enterFrame event is currently locked at 30fps, I tend to just use that event for convenience.

Then you can use your accel() function to just update some global(*) values that get accessed by the master animation function, rather than having animation being driven from two different places in your code.

(*) As you probably know, if you forward-declare values or functions as "local" at the top of your program, you can then treat them like globals throughout the rest of your code, even though they are actually locals and therefore more optimized.

Then, in your master enterFrame function, you can add your tests for level completion, and then turn off the listeners if that condition happens, then handle whatever you need for going to the next level and starting it, etc.

To be clear, none of this is necessarily related to your "main()" function, which could just be for initial setup and launch -- you don't need to actually loop main() or ever really touch it again once the game is running, since your main game loop is most likely driven by your enterFrame function.

In other words, while it's a pretty common convention to call the first function "main", often it's not literally a main loop. (Objective-C takes this to an extreme, and creates a tiny little "main.m" file that does nothing but launch the real code.)
uid: 3007 topic_id: 393 reply_id: 733


[TOPIC: post.html]
#4

garysims

[GLOBAL: userInfoPane.html]
garysims
  • Enthusiast

  • 54 posts
  • Corona SDK

fpassa, evank

Thanks for posting your replies... In the end I used a similar method to what you both suggest. I have a state machine with states for main menu, main menu waiting for tap, level playing, level ended etc etc and I have a control function which is called via performWithDelay. This control function then displays the menus, the screens etc according to the state.

Thanks again for your input...

Gary
uid: 4415 topic_id: 393 reply_id: 746



[topic_controls]
[/topic_controls]