View Full Version : How do I use timers to repeat a function, and what else r they for?
Mythic Fr0st
May 16th, 2007, 06:06 AM
How can I use a timer to repeat a function, and what else do timers "do"
dthought
May 16th, 2007, 08:17 AM
Put the following from the Flash help into an AS file called "TimerExample.as":
package {
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.Sprite;
public class TimerExample extends Sprite {
public function TimerExample() {
var myTimer:Timer = new Timer(1000, 2);
myTimer.addEventListener("timer", timerHandler);
myTimer.start();
}
public function timerHandler(event:TimerEvent):void {
trace("timerHandler: " + event);
}
}
}
Then in your FLA file, in the same location as the AS file:
var myTimerExample:TimerExample = new TimerExample();
senocular
May 16th, 2007, 08:40 AM
(no import unless in a package)
;)
devonair
May 16th, 2007, 10:05 AM
Timers are like suped up intervals.
The thing I like most about them is they accept a repeatCount argument, which is kinda like telling a setInterval() how many times to fire. So if you use the length of an array as a repeat count, for example, you can use a Timer like a slow acting for..each loop.
something like:
var testObject:Object = new Object();
testObject.doSomething = function():void {
trace ("object doing something");
};
var objectArray:Array = new Array(testObject, testObject, testObject);
initTimer();
function initTimer():void {
var timerRepeatCount:int = objectArray.length;
var myTimer:Timer = new Timer(500, timerRepeatCount);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
myTimer.start();
}
function timerListener(te:TimerEvent):void {
// you should always remove event listeners when you're done with them
if (te.target.currentCount == te.target.repeatCount) te.target.removeEventListener(TimerEvent.TIMER, timerListener);
objectArray[te.target.currentCount - 1].doSomething();
}
Mythic Fr0st
May 16th, 2007, 07:18 PM
Instead of using the name TimerExample, could I use timer? for the class name
so it'd be
var t:Timer = new Timer();
not var t:TimerExample = new TimerExample();
Can I?
Got a few q's if ya dont mind
Question 1: Whats the point of packages?
Question 2: Can't I just import every single flash object, so I dont have to import them anymore (if so, how:P)
Question 3: If I wanted to include 3 files, how would I do it, as far as I know #include, doesn't work anymore, you have to name the flash doc's class to the name of the file, but how do I get more than 1 document included in my flash doc?
Mythic Fr0st
May 16th, 2007, 07:24 PM
edit - sorry double post -.-
using dthoughts code, I get this error
**Error** Scene 1, Layer 'Layer 1', Frame 1 : Line 2, Column 1 : [Compiler] Error #1037: Packages cannot be nested.
{
ReferenceError: Error #1065: Variable as is not defined.code I used:
package
{
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.Sprite;
public class TimerExample extends Sprite
{
public function TimerExample()
{
var myTimer:Timer = new Timer(1000, 2);
myTimer.addEventListener("timer", timerHandler);
myTimer.start();
}
public function timerHandler(event:TimerEvent):void
{
trace("timerHandler: " + event);
}
}
}
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.