I tend not to post too much about the innards of the code I’m working with, but I think I ought to, in case it’s useful or interesting to anyone.
I’m currently working on three separate projects in Flash. With two of them, I’ve been able to bury my head in the sand and pretend that my knowledge of ActionScript 2 is up-to-date. With the third one, I’m having to take a partial plunge into ActionScript 3 to avoid complete headaches and stupid lines of code.
Anyone familiar with AS2 might know about the setInterval(); method which ties itself to a function, called after the timer reaches the appropriate time. The problem with this is that it’s hugely cumbersome to combine timers (for example, in my Flash setting for Jena Osman’s Public Figures project, each text transition combined two timers - one a delay, and one a combination of two transition effects, in and out). This can soon lead to very complicated embedded timers in which timer names (which mudt be individually named!) are nastily close to each other on the line.
Check out this code, in AS3. It calls the utils, creates a new variable (in MS) for the timer, tells it to call a function after the time is up, and then defines the function. Sounds similar to the AS2 method above, but importantly the function itself is a lot tidier and makes more sense semanitcally.
import flash.utils.*;
var myTimer:Timer = new Timer(1000);
myTimer.addEventListener("timer", timedFunction);
myTimer.start();
function timedFunction(eventArgs:TimerEvent) {
trace("Event " + myTimer.currentCount + " triggered!");
if (myTimer.currentCount > 5) {
myTimer.stop();
}
}
It also loops by default, so adding the if conditional inside the function says that once it has looped in this case 6 times, stop looping. Changing the 5 to zero will make it happen just the once. Or, you can tell the function to go to another keyframe. Or whatever.
Practical applications in an e-poetic sense? Textual transitions, for one, but importantly, adding a Math:random variable and replacing the timer value with this means that you can easily create randomly timed transitions with only a few lines of code and a more logical-looking layout.
Lee Brimelow’s post on Timers is really useful, and how I learned this.