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");
}

2 comments:

Matthew Taylor 28 July 2013 at 16:45  

I don't want to say "Hello" I just want my function to wait. The function in question adds a circle to the stage, then another in a slightly different place, and so on. I would like to add a child, pause, remove that child and add another. I don't want tweening nor a new movieclip. Please help! Why can't there just be a command called pause?!

iliketo 29 July 2013 at 13:51  

@Matthew,

Please find below an example of a technique I use when I need to enumerate through sequences with pauses. I use a switch statement inside a function and call the setTimeout() method for the pauses. This will also work with the TweenLite.delayedcall() and the Timer class. The setTimeout() is called in each case to create the pause for whatever code you wish to add in the case.

var count:int = 0;

function enumerate():void
{
count++;

switch(count)
{
case 1:
//add code your code here

setTimeout(enumerate, 3000);
break;

case 2:
//add code your code here

setTimeout(enumerate, 3000);
break

}

}

enumerate();

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP