Tuesday, 10 August 2010

Delay function in Actionscript 3

I previously wrote a post called Time delay in Actionscript 3 where I showed different ways of setting a time delay. In this post I will show two ways of delaying a function. You can use the native setTimeout function, or the delayedcall method from the tweenlite package.

Firstly, the setTimeout() function is shown in the example below. There are two parameters in the setTimeout() function. The first one is the function to be executed and second is the delay in milliseconds. So, in the example below a message will be traced out after three seconds. For more information, checkout the AS3 language reference.

//Traces out the “Hello ilike2flash”
function displayMessage(){
trace(“Hello ilike2flash”);
}

//Call the displayMessage function after three seconds.
setTimeout(displayMessage, 3000);

The tweenLite delayedcall method works pretty much exactly how the setTimeout() function works. However, the parameters are the opposite way around and the delay is in seconds. You will of course need to download the tweenlite package for this to work.

//Imports the tweenlite package
import com.greensock.* 

//Traces out the “Hello ilike2flash”
function displayMessage(){
trace(“Hello ilike2flash”);
}

//Call the displayMessage function after three seconds.
TweenLite.delayedcall(3, displayMessage);

You can also use the delayedcall method to call a function after a certain number of frames, or pass in parameters to the function. For more information, click here.

Another method is to use the Timer class. You set a specified a time delay and a repeat count of 1.This will call the showMessage() function after three seconds.
var myDelay:Timer = new Timer(3000,1);
myDelay.addEventListener(TimerEvent.TIMER, showMessage);
myDelay.start();

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

0 comments:

  COPYRIGHT © 2011 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP