| Custom timing technique and optimization with setInterval |
| Written by Petar Jercic | ||||
Page 1 of 2
Custom timing technique and optimization with setIntervalIf you are making your logo that will be incorporated in many different flash games, or if you want to optimize your flash game or movie, setInterval function is your friend. Not only can you play your clips independent of the given frame rate, you also can optimize which objects on screen are more important so you can give them more processor time than unimportant ones. Let’s begin. Imagine we made a logo and created a little animation method that dims our logo every frame so it disappears and reappears at different place. You would use this actionscript code.
We want it to be visible just enough so the user can click on the logo (I used our ArtCode Games logo :).
But in the second example imagine developer used 60 fps so the user can’t click on the logo because it’s too fast. What to do then? Do we tell the developers at what fps to make their game? No the solution is in setInterval function. What is this setInterval function?
Basically it works like execute method_to_execute every n milliseconds. It returns ID of the interval which we can remember or not. If we remember it we can remove this interval using clearInterval method.
And that is enough to fix our previous example. Using setInterval function we can separate display of our logo from frame rate or the game. The code goes like this.
If you check the frame rate it is the same as before, but the logo changes the same speed. That is because the function in both movies is called every 40 milliseconds, that is 25 times every second. So that means our logo constantly has frame rate of 25 fps. |