Hi folks,
I would like to discuss about how you can avoid slowdowns in your game loop. Means how to avoid that the framerate drops below 30 FPS.
Atm I am working on an arcade/action game. The gameplay requires that the framerate stays at 30 FPS each frame.
During gameplay I have one background sprite (320x480), one GUI sprite (320x32) and 5-30 sprites with a size of 32x32 on the screen. 5 sprites are always visible and stationary, 10 sprites appear depending on the player input and 10-15 are particles which are handled by the game logic.
For all movements and animation I use TRANSITION.TO commands, which have onComplete functions that also call a TRANSITION.TO method. So for each method I use this approach:
- Span object, move it via transition.to (including an onComplete handler)
- On complete call another transition.to to handle alpha,size and rotation (including an 2nd onComplete handler)
- On 2nd complete, remove the object.
At first I was creating each object on the run via display.newImage and removed it when not needed anymore.
As you can imagine, my framerate was jumping sometimes when I created 10-20 images (particles) in one frame.
Now I preload all the images, store them in a group and have them invisible at first.
When I spawn an object now, I loop throught the group to find the first which is invisible and bring it on the screen. When I want to remove it, I just set it to invisible.
In my enterFrame event handler, I just do collision checks and act on them. For this I loop again through the groups and just check for visible ones, which will be checked for collision. Collisionchekc is done via simple Circle2Circle check or Box2Box checks.
Another thing I did was that I precalculated random-tables to avoid costly math.random calls.
Here is a summary of my speedup methologies:
- Preload the images, set them to invisible. Just placing them outside the screen doesn’t help. Or set alpha to 0.0, the engine handles this the same as object.isVisible=false
- Use precalculated tables for math.random calls
- Use transition.to calls
- Create function headers like this local myfuncname = function()
- Use locals as much as possible, they are faster than globals. Even in the first level.
I thought that transitions would help big time, but now I am not so sure anymore and will revert the movement and animation handler back into the enterframe function for a test to handle the movement and animation there manually. Maybe it is because of the transition bug I found, but they should be much faster than something that is coded in LUA.
Anyway, I still experience dropping of the framerate. Can you share some of your tips, how to avoid this? Or is Corona only be useable for puzzle games or games that don’t involve 20-40 moving elements? Btw, I test on an IPod touch 2.Generation. I don’t think that the device is that limited regarding objects.
Have a play of geoDefense (lite) and you can see what I mean.
Any input to this is much appreciated.
Michael Hartlef
[import]uid: 5712 topic_id: 848 reply_id: 300848[/import]