Optimizing the enterframe function?

Hey guys I was wandering what would be the best way to use the ‘enterframe’ listener on lots of diferent functions…?
So so for example I want to call a 4 functions as fast as possible; update the score, move the player, move platform, move background. What is the optimized way to write this?
All in one code,
all in seperate functions and called individualy from the one ‘enterframe’ function,

each with their own enterframe call,

or on the last line of each function it calls the next function on the list making a cycle?

At the moment I am using the one enter frame listener and calling all the individual functions

[lua]local movePlatform= function()

end

local movePlayer = function()

end

local updateScoreboard = function()

end

local moveBackground= function()

end

local enterFrame = function()

    movePlayer()

    movePlatform()

    updateScoreboard()

    moveBackground()

end[/lua]

Not many calls here, but with time I will be adding more (no doubt) so taking that into account too.

Your code above is just fine. You can certainly add more.

What you need to be careful with is how much you require these functions to do. Remember that enterFrame will be called on every refresh, so your game will get jittery and out of sync with itself if you do too much.

Two thing you can do to improve performance:

#1 - Hand as much to lower level functions as possible - animations can often be handled with transitions, for example. Just remember to keep a reference to the transition so you can cancel it if necessary.

#2 - Try to have dirty flags on things which get updated. For example, if you update the score with a new group and a number of images added to that group, make sure that when the actual in-memory score value is update - and only then - you set the scoreIsDirty flag. Then, in enterFrame, you can check that flag and only perform the update if the isDirty is true.

Note: #2 is definitely not the best way to do that type of update. In general, if you are updating elements of your display and find they should only be updated occasionally, you definitely don’t need to have them handled by enterFrame. In your list of functions above I would actually remove updateScoreBoard() because it should only update when the score changes.

The main thing is what the functions do. I can call 100 functions every enterFrame if they don’t do much. The slow down happens when the functions being called are slow. Keep putting as many as you want into the enterFrame (of course, only if it NEEDS to update every frame), and just make sure your functions have optimized loops etc.

For reference, I call 32 functions every single frame and I can get very good frame rates out of my game, even with tons happening on screen. Shameless self promo (Super Balloon Rush on iOS http://georiot.co/jv0)

Your code above is just fine. You can certainly add more.

What you need to be careful with is how much you require these functions to do. Remember that enterFrame will be called on every refresh, so your game will get jittery and out of sync with itself if you do too much.

Two thing you can do to improve performance:

#1 - Hand as much to lower level functions as possible - animations can often be handled with transitions, for example. Just remember to keep a reference to the transition so you can cancel it if necessary.

#2 - Try to have dirty flags on things which get updated. For example, if you update the score with a new group and a number of images added to that group, make sure that when the actual in-memory score value is update - and only then - you set the scoreIsDirty flag. Then, in enterFrame, you can check that flag and only perform the update if the isDirty is true.

Note: #2 is definitely not the best way to do that type of update. In general, if you are updating elements of your display and find they should only be updated occasionally, you definitely don’t need to have them handled by enterFrame. In your list of functions above I would actually remove updateScoreBoard() because it should only update when the score changes.

The main thing is what the functions do. I can call 100 functions every enterFrame if they don’t do much. The slow down happens when the functions being called are slow. Keep putting as many as you want into the enterFrame (of course, only if it NEEDS to update every frame), and just make sure your functions have optimized loops etc.

For reference, I call 32 functions every single frame and I can get very good frame rates out of my game, even with tons happening on screen. Shameless self promo (Super Balloon Rush on iOS http://georiot.co/jv0)