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.