Using the Timer Object
So far we've controlled cyclical actions in one of two ways: either through an ENTER_FRAME listener-callback arrangement, which we used for visible animation, or through some some logical structure such as FOR or WHILE, for iterative processes that don't need to be visible.
Here's a very useful third option: the Timer object, which gives you access to your PC's internal clock. Note that with Timer, you can synchronize events in Flash movies independently of the frame rate (up to a point).
I. Simple blinking text
The example above shows perhaps the simplest illustration of Timer effects: blinking text. Here's how it's done, using a very simple document class:
package
{
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.display.MovieClip;
public class timerEx_1DocClass extends MovieClip
{
var blinkTimer:Timer = new Timer(1000,0);
function timerEx_1DocClass():void
{
blinkTimer.addEventListener(TimerEvent.TIMER, blink);
blinkTimer.start();
}
function blink(event:TimerEvent):void
{
var theRoot:MovieClip = MovieClip(root);
if(theRoot.tf.text == "")
{
theRoot.tf.text = "It's time to feed the cats!"
}
else
{
theRoot.tf.text = "";
}
}
}
}
In addition to the MovieClip and TextField imports, which we need in order to work with the main movie and a text field defined there, respectively, we import flash.utils.Timer in order to work with the Timer object, and flash.events.TimerEvent in order to handle events related to that object.
There is only a single declared, public variable, blinkTimer, which is an object variable hosting an instance of the Timer object. Note the arguments to the object's Constructor method, Timer(). The value 1000 means our Timer will expire every 1000 milliseconds (that is, every second). This value is not affected by the playback rate of the movie. The zero in the second position means that this Timer will run indefinitely. In effect, zero equals infinity. If you want to set a limit on the number of repetitions for a given Timer, use some number greater than zero.
In terms of methods, we have only a Constructor and a single event-callback. In the Constructor, we add an Event Listener for the Timer Object, which is basically a virtual alarm bell that rings whenever our 1000 milliseconds are up. The listener hands off to a callback named blink().
We also call the start() method of our Timer object, which does about what you'd expect. (There is also a stop() method, if you ever need to terminate a Timer before its programmed expiration.)
The custom method blink() is our callback. At the top of this function, we assign the conversion MovieClip(root) to a utility variable called theRoot, which saves us some typing. By now, you should know why we need to know where the root is.
Next, we perform an if/else check on the current value of a text field named tf, which was pre-built into the main movie. If there's nothing in this field, we insert a message. Or vice versa.
The result is what you see, or, If Cats Could Code.
II. Blinking text with count
Meanwhile, here's a small variation on the first demo. Watch for several blinks:
In this version, we're using a property of Timer called currentCount, which keeps track of how many times the Timer has repeated. We've modified the blink() method to take advantage of this information.
function blink(event:TimerEvent):void
{
var theRoot:MovieClip = MovieClip(root);
var theText:String;
if(blinkTimer.currentCount % 5 == 0)
{
theText="Reminder number "+ numNudges + ".";
}
else
{
theText = "It's time to feed the cats!";
numNudges ++;
}
if(theRoot.tf.text == "")
{
theRoot.tf.text = theText;
}
else
{
theRoot.tf.text = "";
}
}
Note that the variable numNudges, which we use to differentiate the on (text) blinks from the off blinks, is declared at the top of the class file, with blinkTimer. We have to do this so we can give it an initial value.
The currentCount property comes into play in that new if/else structure, with the test:
if(blinkTimer.currentCount % 5 == 0)
This statement has nothing to do with percentages. The % symbol in ActionScript represents the modulo operator, which returns the remainder of an integer division of the second term (here, 5) into the first term (blinkTimer.currentCount). In other words, this operator lets us count by fives, as it will return true whenever the current number of blink repetitions is a multiple of 5 (e.g., at 5, 10, 15, 20, and so on).
III. Source files
You've seen all the code there is for these examples, but if you'd like the source files for futher development, they're in the timer subdirectory in MULTIMEDIA on student-iat.
|
|