Skip to main content
Inspiring
June 23, 2009
Question

inserting a delay between lines of code

  • June 23, 2009
  • 1 reply
  • 936 views

I looked at the Timer function but i guess i am not using it right.

I have something like this:

var mytimer:Timer = new Timer(1000, 1);

addChild(blah);

mytimer.Start();

addChild(blah2);

I was hoping i'd see a 1 second delay between the first and 2nd movieclips being added, but i'm not.

What am i doing wrong? How do i insert a delay between actions?

thanks

This topic has been closed for replies.

1 reply

June 23, 2009

Just by adding a start statement for timer do not stop the execution you will have to modify the code as below:

var mytimer:Timer = new Timer(1000, 1);


addChild(blah);
mytimer.Start();
mytimer.addEventListener(TimerEvent.TIMER,addSecondMC)

function addSecondMC(evnt:TimerEvent)
{
mytimer.stop();
mytimer.removeEventListener(TimerEvent.TIMER,addSecondMC);
addChild(blah2);
}

merkkAuthor
Inspiring
June 23, 2009

wow, that seems like a lot of code for something as simple as inserting a delay in code execution. I'm surprised there isn't something where you can just insert a X millisecond delay.

I will give your code a try a little later today and post back if it works. thanks for your help.

June 23, 2009

>>wow, that seems like a lot of code for something as simple as inserting a delay in code execution.

Well... if you look at what you had, versus the proper way - it really only adds 4 lines.

And you could use setTimeout, though Timer is preferred:

addChild(blah);

setTimeout(delayedAddChild, 1000);

function delayedAddChild(){

     addChild(blah2);

}