03.04.09
Have you ever had the need to make a line of code wait a second before executing? How did you go about trying to make that happen? Well, I for one did a Google search and came up with something using setInterval (AS 2.0), or using setTimeout (AS 3.0). Â Either solution will work, and probably looked something like this:
var wait1 = setTimeout(callback1, 3000);
function callback1():void {
trace("Wait 1");
};
The code above told Flash to wait three seconds before calling callback1, which traces "Wait 1". Â So, what if you needed to make two lines, or two blocks, or multiple blocks of code, you might have something that looked like this:
var wait1 = setTimeout(callback1, 1000);
function callback1():void {
trace("Wait 1");
};var wait2 = setTimeout(callback2, 2000);
function callback2():void {
trace("Wait 2");
};var wait3 = setTimeout(callback3, 3000);
function callback3():void {
trace("Wait 3");
};
Notice you have to manually add up the time delay to get the individual callbacks to fire at the right time. Â So, it'd be a great inconvenience if you needed to adjust the delay for wait 1, since you'll need to adjust the wait time for wait 2 and 3 as well. Â Plus, it's a hideous looking block of code. Â So, out of the goodness of our hearts, and for the benefit of all developerkind, we, Plexipixel, are going to make public one of the most useful utilities in our library, our Script class.
(Okay, it's not really for the benefit of mankind, or out of the goodness of our hearts, it's more like we wanted to drive some web traffic to our blog. Â So, thanks for visiting! Â Now, buy something, thank you.)
You can download the source code with examples here. Â The following code will do that same thing as the code above:
var cs:Script = new Script;
cs.call(this, function() {
trace("Start Script");
});
cs.wait(1.0);
cs.call(this, function () {
trace("Wait 1");
});
cs.wait(1.0);
cs.call(this, function() {
trace("Wait 2");
});
cs.wait(1.0);
cs.call(this, function() {
trace("Wait 3");
});
cs.start();
Besides being easier to read, you don't have to add up time. Â For space constraints I won't reprint the Script class itself here, but I will run through how to use it.
After you've imported the Script class and instantiated a Script object, you use wait, to add a wait, it takes time in seconds, so, half second would be 0.5.  Use call to call a function.  call takes two parameters, the first tells Script the scope of the function to be called.  The second parameter is the function to call, in the example above I used an inline function.  To call a function do the following:
var cs:Script = new Script;
cs.call(this, testFunction1);
cs.start();
function testFunction1():void {
trace("testFunction1 called");
}
So, what if you wanted the your application to wait for an indefinite time, like waiting for a MovieClipLoader to load something? Â We have that covered too:
var cs:Script = new Script;
var i:Number = 0;
cs.call(this, function() {
trace("i " + i);
i++;
if (i < 10) return false;
trace("Done");
});cs.start();
If you manually include a return false in your application it'll force the Script class to loop indefinitely, until you stop sending false, or send it a true.
Anyway, we hope you find this little utility helpful in your Actionscript endeavors.
Allen is a Flash Developer here at Plexipixel. When not hard at work, he enjoys long walks on the beach (no, not really), and journaling by the fireside (definitely not).

Leave a Reply