Newest Articles

MegaCombs
Flash Media Player
XML Driven Pie Chart
Base Defender
Hangman Game
8 Ball Pool


Popular Articles

True Fullscreen Flash Mode
Catapult Game
Hangman Game
Infinitely Zooming Image
3d Rotating Image Cube
Base Defender


Random Articles

Gravitas
Photo Reel
Storing Data Similar to Cookies
True Fullscreen Flash Mode
Disappearing Button
Flash Media Player


Links

Shapes the Game
Reddit
Newgrounds
TWiT
Link to SwfSpot
Swf Spot

Contact me on Google+



rss feed

onEnterFrame vs setInterval

onEnterFrame vs setInterval
AddThis Social Bookmark Button
Description: Using setInterval instead of onEnterFrame to execute code at different intervals of time
Author: John Bezanis
Added: November 14th 2007
Version: Flash 8


onEnterFrame is a popular method to have code run at certain intervals of time, but it has a drawback. onEnterFrame's time intervals are equal to the frame rate set in the document properties. If you want to run some code at different intervals of time, onEnterFrame won't be able to do the job. This is where setInterval is used. setInterval works like onEnterFrame except the time intervals are independent of the document's set frame rate. The timer measures the intervals in milliseconds, so you can run a function many times faster or slower that the document's frame rate.
Example: We have two boxes, one using onEnterFrame and one using setInterval. The document is set to 30 frames per second. The box using setInterval is set to execute every 100th of a second.
Initializing the two boxes to 0:
onenterframebox=0;
setintervalbox=0;

Code for onEnterFrame:
onEnterFrame=function(){ onenterframebox++; }
Function that will run at each interval:
function intervalfunction():Void { setintervalbox++; }
When creating the actual interval, intervalid is an id associated with the interval, the first parameter "this" is the object to associate with the interval, intervalfunction is the function that gets called at each interval, and 10 is the number of milliseconds between each interval.
var intervalid:Number = setInterval(this, "intervalfunction", 10);
To remove the interval, use clearInterval(intervalid);
The source code of the example is available below:

Download the Source File
Comments Currently Disabled