Monday, 27 July 2009

Time delay in Actionscript 3

In Actionscript 3.0 there are two ways of setting a time delay. Firstly, there is the setInterval function from AS2, or there is the Timer class in AS3. The setInterval function is not a reliable method as the results are based on the frame rate of the flash document, and also the memory of the movie clip. So, the fps of the flash document will have an effect of the accuracy of the time delay.

Below is an example of how to use the setInterval function. In Actionscript 3.0 you need to add the flash.utils package. The first parameter of the setInterval function is method that will be executed, and the second is the delay in millisecond.

import flash.utils.*;

setInterval(showMessage,3000);

function showMessage(){
trace("hello");
}

The code above calls the function ‘showMessage’, which will display the message ‘hello’ appropriately every three seconds. If you want to stop the message after one delay of three seconds you can use the ‘clearInterval’ function shown below.

import flash.utils.*;

var test = setInterval(showMessage,3000);

function showMessage(){
trace("hello");
clearInterval(test);
}

The Timer class for setting a delay in Actionscript 3.0 is more robust then using the setInterval function, but still has the accuracy issues with the frame rate the memory. Below is the code for setting a time delay:

var myDelay:Timer = new Timer(3000);
myDelay.addEventListener(TimerEvent.TIMER, showMessage);
myDelay.start();

function showMessage(event:TimerEvent):void{
trace("hello");
}

The code above adds a new instance of the Timer class with a delay of three seconds. The event listener has the TimerEvent class and the parameter showMessage which display the message ‘hello’.

Below is an example of a time delay with the Timer and TimerEvent classes. The red square moves 50 pixels to the right every two seconds.




Another technique of setting a time delay is to use the getTimer() method. This method calculates the number of milliseconds the have passed since the start of the movie playing. So by initially setting a getTimer() method at the start of a movie we can set a time delay by subtracting the current millisecond passed by the start time. Below is an example of using the getTimer() method to trace out a message after 5 seconds.

var startTime=getTimer();

stage.addEventListener(Event.ENTER_FRAME, timeDelay);

function timeDelay(event:Event):void {
var timePassed=getTimer();
if (timePassed-startTime >= 5000) {
trace("Hello ilike2flash");
}
}

6 comments:

AliennoisE 24 February 2010 at 16:39  

I like how this code works. Thanks. Is it possible for you to show an example how u use it with tween classes? Thx.

iliketo 25 February 2010 at 02:45  

Since you want to cause a delay in the tween. You would have to add the code inside the function. I have also have a tutorial on the tweens which can be found here

Mohammed Zubair 23 September 2012 at 03:40  

can you please help me out to give a delay after "this.gotoAndStop(currentFrame + 30);" statement executive..!

import flash.display.FrameLabel;
var myMic:Microphone = Microphone.getMicrophone();
myMic.setUseEchoSuppression(true);
myMic.setLoopBack(true);
var st:SoundTransform = new SoundTransform(0);


this.addEventListener(Event.ENTER_FRAME,startSpeaking);// creates enterframe event
function startSpeaking (e:Event)

{


if(myMic.activityLevel== -2 || myMic.activityLevel <= 80 || myMic.silenceLevel <= 99)

{
stop();
myMic.soundTransform = st;
//myDelay.start();
myMic.setSilenceLevel(100,9000);

}
else
{

this.gotoAndStop(currentFrame + 30);
myMic.soundTransform = st;
myMic.setSilenceLevel(0,1000);
}

}



stop();

iliketo 23 September 2012 at 09:44  

@Mohammed,

Use the setTimeout() method.

Blogger_Fool 8 May 2013 at 22:47  

I needed a movie clip object of mine to stop after it is finished and resume playing after a timed delay at the end of the clip. This was what I managed to put down that worked!

(stick this at an actionscript frame at the end of the movie)

function playdelay():void
{
play();
}

stop();

setTimeout(playdelay, Math.random()*3500);

here it will wait a random amount of time up to 3.5 seconds before resuming to play the movie clip object timeline.

iliketo 10 May 2013 at 14:41  

@Blogger_fool

I tend to use the following code below, but your code will also work.

stop();

setTimeout(play, Math.random() * 3500);

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP