Develop time functions with Flash ActionScript 2.0

June 17th, 2008

Discuss two ways that you can develop timing in Flash. What are the advantages to each?

Using the native onEnterFrame() or setInterval() functions are two ways to develop timing.

The onEnterFrame() executes as fast as your Frames per Second.

The following example I made a movie clip and named it mc with alpha set to 0:

mc.onEnterFrame = function()
{
if(mc._alpha < 100) {

mc._alpha += mc._alpha+1;
}

else
{
delete this.onEnterFrame;
trace("done");
}

}

/*  OUTPUT  **************************

mc alpha's in from alpha = 0 to alpha = 100

************************************ */

ActionScript 2.0 allows custom behaviors for movie clips that previously one would need to code into the "onMovieClip(enterFrame) handler. Therefore, you can have multiple movieclips with different characteristics but still exhibit similar behavior. The onEnterFrame method can be used to calculate the positions of a movie clip instance.

The setInterval() runs a function at a specified interval (in milliseconds) and can be modified to execute it's timing in any second or fraction thereof. (3.0 uses the "Timer" class instead of setInterval.) The setInterval() function allow you to set your own time-based event, similar to onEnterFrame(), executing repetively over time. And, setInterval returns object instead of interval id for better OOP structure.

The following example calls the "executeCallback" function every 3000 milliseconds (every 3 seconds).

var intervalId:Number;
var count:Number = 0;
var maxCount:Number = 10;
var duration:Number = 3000;

function executeCallback():Void {
trace("executeCallback intervalId: " + intervalId + " count: " + count);

if(count >= maxCount) {
clearInterval(intervalId);
}
count++;
}

intervalId = setInterval(this, "executeCallback", duration);

/*  OUTPUT  **************************

executeCallback intervalId: 1 count: 0

executeCallback intervalId: 1 count: 1
executeCallback intervalId: 1 count: 2
executeCallback intervalId: 1 count: 3
executeCallback intervalId: 1 count: 4
executeCallback intervalId: 1 count: 5
executeCallback intervalId: 1 count: 6

executeCallback intervalId: 1 count: 7
executeCallback intervalId: 1 count: 8
executeCallback intervalId: 1 count: 9
executeCallback intervalId: 1 count: 10

************************************ */

** References **

http://livedocs.adobe.com/flash/8/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00002490.html
http://livedocs.adobe.com/flash/8/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001766.html
http://www.adobe.com/devnet/flash/articles/mc_subclasses_v2_03.html
http://www.actionscript.org/forums/archive/index.php3/t-95283.html


Entry Filed under: All, Flash, Tutorial

Leave a Comment

You must be logged in to post a comment.

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

June 2008
M T W T F S S
« May   Jul »
 1
2345678
9101112131415
16171819202122
23242526272829
30  

Most Recent Posts