AS3: Timer
- Denvish
-
Denvish
- Member since: Apr. 25, 2003
- Offline.
-
- Send Private Message
- Browse All Posts (15,977)
- Block
-
- Forum Stats
- Member
- Level 46
- Blank Slate
Although you can still use setInterval and setTimeout in AS3, they will produce an annoying compile error, so you're better off (or so the help files will tell you) using the new Timer class.
The basic function of the Timer is that it allows you to set up a... well, timer, that runs for a specified number of times at a specified speed
To set up the timer, you'll need something like this:
var DTimer:Timer = new Timer(33, 0);
The first parameter is how often, in milliseconds, to run the timer - 33 = ~30FPS
The second parameter is how many times to tun the timer. If you only want it to run 10 times, every half a second, use:
var DTimer:Timer = new Timer(500, 10);
Once you've set up your timer, you'll need to specify what it should do. This involves (like pretty much everyting else in AS3, it seems) setting up an Event Listener:
DTimer.addEventListener(TimerEvent.TIMER, DRAWLINE);
The only thing of interest in this line of code is DRAWLINE, which is the function we're going to call on each timer event. Having only been using AS3 for a couple of days myself, I haven't figured out yet whether it's possible, or how to, send parameters to the function. I'm sure that'll be covered elsewhere.
Now we've done that, let's define a y variable and make the function
var yy:Number=0;
function DRAWLINE (event:TimerEvent):void{
trace ("Lookit me ma, I'm drawing a line!");
graphics.lineStyle (3,0xFF0000,1);
graphics.moveTo (0,yy);
graphics.lineTo (stage.stageWidth,yy);
yy+=30;
if(yy>stage.stageHeight){
DTimer.stop();
}
}
Couple of things to note here:
Firstly, alpha range for lineStyle in AS3 is now 0-1, not 0-100.
Secondly, Stage.width is now stage.stageWidth, and the same for height.
Thirdly, the drawing API commands now need to be preceded by graphics., otherwise you'll get a compiler error.
Finally, we need to command the timer to start. You could do that with a mouse event or whatever, for the sake of clarity here, we'll just run it after the rest of the code
DTimer.start();
So here's the entire code block. Paste it into a new AS3 fla to test it
//=====================
import flash.display.Graphics; //FOR THE DRAWING API
import flash.display.Stage; //FOR STAGE REFERENCES;
import flash.utils.Timer; //TIMER
import flash.events.TimerEvent; //TIMER EVENT
var yy:Number=0; //(STARTING) Y POSITION OF LINE
//FIRST VALUE IS NUMBER OF MICROSECONDS BETWEEN TIMER EVENTS
//SECOND VALUE IS NUMBER OF TIMES TO RUN TIMER EVENT - 0=INFINITE
var DTimer:Timer = new Timer(33,0); //CREATE THE TIMER
DTimer.addEventListener(TimerEvent.TIMER, DRAWLINE); //ADD THE TIMER EVENT
function DRAWLINE(event:TimerEvent):void{
trace("Lookit me ma, I'm drawing a line!");
graphics.lineStyle (3,0xFF0000,1); //BE AWARE ALPHA RANGE IS NOW 0-1, NOT 0-100
graphics.moveTo (0,yy); //MOVE TO LEFT
graphics.lineTo (stage.stageWidth,yy); //Stage.width IS NOW stage.stageWidth
yy+=30; //MOVE DOWNWARDS
if(yy>stage.stageHeight){ //REACHED BOTTOM OF STAGE
DTimer.stop(); //STOP THE TIMER EVENT
}
}
DTimer.start(); //RUN THE TIMER
//=====================
You can also access the number of times a timer event has fired by using DTimer.currentCount;, and you can reset the timer back to zero and stop it with DTimer.reset();. You can find out whether a timer is running by accessing the boolean DTimer.running;, and if you specify a number for the delayCount (the second parameter in the timer setup, in my example I use infinity (0)), you can use the timerComplete event when the the timer reaches its last count.
- Fion
-
Fion
- Member since: Aug. 21, 2005
- Offline.
-
- Forum Stats
- Member
- Level 39
- Blank Slate
Holy hell man, AS3 is looking like a whole new swimming pool and it seems like you yourself are still just testing the waters. Well nice tutorial all the same, well presented as usual and I wish you luck with the new AS3: Main.
.
- dragonjet
-
dragonjet
- Member since: Dec. 2, 2005
- Offline.
-
- Forum Stats
- Member
- Level 20
- Blank Slate
whow! gotta question, is AS3 in flash 9? i mean... wait...
now that flash is adobe's...
is adobe flash 9 on sale today? and its running on AS3?
what i mean here is the flash 9 maker, not the flash player....
flash studio it is... studio 9....??
- EvanHayes
-
EvanHayes
- Member since: Jan. 13, 2007
- Offline.
-
- Forum Stats
- Member
- Level 11
- Blank Slate
im beggining to warm up to thios language :D
but nice tutorial :D
Grah i feel so unknown, SK8MORE god damn :/ EvanHayes seems like a much more serious name than sk8more,so i changed it.
- Pyrofire133
-
Pyrofire133
- Member since: Apr. 23, 2007
- Offline.
-
- Forum Stats
- Member
- Level 04
- Game Developer
odds are good its going to be:
object.AddEventListener(EVENT, Function, Param1, Param2, ..., Param#);
- Denvish
-
Denvish
- Member since: Apr. 25, 2003
- Offline.
-
- Send Private Message
- Browse All Posts (15,977)
- Block
-
- Forum Stats
- Member
- Level 46
- Blank Slate
At 5/2/07 10:04 AM, Pyrofire133 wrote: odds are good its going to be:
object.AddEventListener(EVENT, Function, Param1, Param2, ..., Param#);
Yeah, that would be nice, wouldn't it? But OH NO THAT'D BE WAY TOO EASY
public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
Guess I'll have to spend another five hours Googling to find out how to do one of the most basic actionscript fundamentals
- Depredation
-
Depredation
- Member since: Sep. 5, 2005
- Offline.
-
- Forum Stats
- Member
- Level 17
- Game Developer
Wow, from setInterval(function, time) to all that, quite a leap. Still though, i'll probably learn it, you can do some nifty shit with it :). Good tutorial, per usual ;).
- West-End-Pro
-
West-End-Pro
- Member since: Feb. 15, 2006
- Offline.
-
- Forum Stats
- Member
- Level 24
- Blank Slate
Wow, that looks uber different to AS2...Quess i'll have to learn another coding language :(
- EglStrk
-
EglStrk
- Member since: Feb. 20, 2007
- Offline.
-
- Forum Stats
- Member
- Level 21
- Blank Slate
You should get a book on AS3. It's a lot easier than using the internet.
~There is never a good excuse for doing nothing. God grants liberty to those willing to stand for it!
- crushy
-
crushy
- Member since: Sep. 17, 2005
- Offline.
-
- Forum Stats
- Member
- Level 15
- Blank Slate
At 5/2/07 03:02 PM, EglStrk wrote: You should get a book on AS3. It's a lot easier than using the internet.
There are probobly reasons, but how could someone write a book about a code they have to completely learn inside-out in like 2 weeks, then writew the book and everything...
- EglStrk
-
EglStrk
- Member since: Feb. 20, 2007
- Offline.
-
- Forum Stats
- Member
- Level 21
- Blank Slate
At 5/2/07 03:05 PM, crushy wrote:At 5/2/07 03:02 PM, EglStrk wrote: You should get a book on AS3. It's a lot easier than using the internet.There are probobly reasons, but how could someone write a book about a code they have to completely learn inside-out in like 2 weeks, then writew the book and everything...
A lot of developers, and people of the sort, get that kind of information early.
~There is never a good excuse for doing nothing. God grants liberty to those willing to stand for it!
- Fatal
-
Fatal
- Member since: Jul. 7, 2005
- Offline.
-
- Forum Stats
- Member
- Level 13
- Blank Slate
This may sound like a stupid question.
But i know like the very basics of AS
And i was wondering if i should become more experienced with AS before i venture onto AS3... Or should i go straight into AS3?
- Snubby
-
Snubby
- Member since: Dec. 4, 2004
- Offline.
-
- Forum Stats
- Member
- Level 21
- Game Developer
all those capital letters scare me... I don't like AS3.
- Wurmy
-
Wurmy
- Member since: Jun. 20, 2006
- Offline.
-
- Forum Stats
- Member
- Level 28
- Programmer
At 5/2/07 04:41 PM, Fatal wrote: And i was wondering if i should become more experienced with AS before i venture onto AS3... Or should i go straight into AS3?
From the looks of this AS3 is pretty much COMPLETELY different from AS2 so I'd say just go into AS3.
Boy, can't wait to get CS3 <<
- Fatal
-
Fatal
- Member since: Jul. 7, 2005
- Offline.
-
- Forum Stats
- Member
- Level 13
- Blank Slate
At 5/2/07 04:43 PM, Wurmy1029 wrote:At 5/2/07 04:41 PM, Fatal wrote: And i was wondering if i should become more experienced with AS before i venture onto AS3... Or should i go straight into AS3?From the looks of this AS3 is pretty much COMPLETELY different from AS2 so I'd say just go into AS3.
Boy, can't wait to get CS3 <<
Yeah i think i will.
I came in my pants when i realised it syncs with photoshop!! I use that for flash much much more than fireworks XD
- crushy
-
crushy
- Member since: Sep. 17, 2005
- Offline.
-
- Forum Stats
- Member
- Level 15
- Blank Slate
Nice tut, I made a little 60 seconds tick tock thingy! :D
var myTimer:Timer = new Timer(1000, 60);
var tickTock:Boolean = new Boolean();
var a:int = new int();
myTimer.start();
myTimer.addEventListener(TimerEvent.TIMER, timer);
function timer(Event:TimerEvent)
{
a = a + 1;
if (tickTock == false)
{
trace("Tick " + a);
tickTock = true;
}
else
{
trace("Tock " + a);
tickTock = false;
}
}
Timers rule!
- falariem
-
falariem
- Member since: Aug. 19, 2008
- Offline.
-
- Forum Stats
- Supporter
- Level 28
- Programmer
Wow! Cool tutorial, and a very handy piece of code.
Personally, I hated using setInterval() and setTimeout(). There was never any clear indication that the script was working, or that you were even moving in the right direction... save for a few trace statements. And using conditionals to clear out the timeout was frustrating and tedious, not to mention unreliable at best.
This Timer class, on the other hand, was a piece of cake! The AS3 compile errors definitely steered me in the right direction whenever my script would not work. And the fact that you can set the amount of iterations... Well, if you can't tell, I'm excited about the possibilities.
Thanks again!
- Rhinan
-
Rhinan
- Member since: May. 19, 2005
- Offline.
-
- Forum Stats
- Member
- Level 15
- Game Developer
At 5/2/07 05:07 AM, dragonjet wrote: whow! gotta question, is AS3 in flash 9? i mean... wait...
now that flash is adobe's...
is adobe flash 9 on sale today? and its running on AS3?
what i mean here is the flash 9 maker, not the flash player....
flash studio it is... studio 9....??
Now that's confusing.



