- Home
- Tutorials
- Flash
- Intermediate
- Delaying Actions

Page 2 of 2
Jesse Stratford
Jesse lives and works in Melbourne Australia. He is the Cofounder of http://ActionScript.org. A Flash enthusiast, teacher, author, freelancer and speaker Jesse's main focus nowadays is managing http://ActionScript.org, but he enjoys participating actively in community and the wider Flash scene when he has time.
View all articles by Jesse Stratford- Perform these actions every time the frame changes (even if your _root level is stopped these actions will still continue to be performed 12 times a second, assuming your fps is 12).
- If the _root level variable delay is true;
- If the _root level variable waitHowLong is less than or equal to the current time in seconds minus the time at which we began counting
- Comment - Insert your actions on this line
- Set the _root level variable delay to False.
- Else for like 3's If
- Comment just for neat coding purposes
- Close Line 6's Else
- Close Line 2's If
- Close Line 1's Clip Event.
So you can see that, given a few variables, this code will loop until the time is right, then execute the actions on Line 4. Those variables are:
_root.delay - Boolean variable, marks if we are running a delay loop at the moment. This allows us to save on CPU usage if we are not currently running a delay, because if delay = false the rest of the actions in this code are skipped
_root.waitHowLong - A number variable (Integer or Floating point) which states the time in seconds we wish to wait before carrying out Line 4's actions
_root.startTime - Floating point variable set upon initiation of delay, see below.
Triggering:
Having now setup the engine which will control our delay we need to give it some info. This trigger info can be sent from a keyframe or a button. Let's look at a button:
on (release) {
_root.delay = true;
_root.startTime = (getTimer()/1000);
}
This code, placed upon a button will set the delay variable to true and thus initiate our delay loop. It also sets the startTime variable (to the current number of seconds since the movie began). So in order for this to work you also need to put this code somewhere:
_root.waitHowLong = 5;
(This makes a 5 second delay).
The best thing about this script is that it allows you to change the length of the delay dynamically, as shown here:
on (release) {
_root.waitHowLong = random(10)+1;
_root.delay = true;
_root.startTime = (getTimer()/1000);
}
This button will create a delay of a random number of seconds.
Known Limitations:
Like they say in Seinfeld, "1 Chip, 1 Dip!". If you want to run more than one concurrent delay you will need more than one controller movie clip and you will also need to create a whole new set of variables (delay2, startTime2, etc)
That's it really!
| Jesse Stratford is the Co-Master of ActionScript.org and a freelance Flash developer and teacher. He is based in Australia and enjoys all things Flash. NB: If you have comments or feedback please feel free to email me, but please do not email me Flash questions; the forums are provided for that purpose and you will get a faster answer by posting you question there. |
If you have found this tutorial helpful, I hope that you will take 30 seconds to visit The Hunger Site where, with just one click you can make a free donation of food to a starving person in a third-world country. We do not benefit financially from this action; it is purely an act of charity. |
| This tutorial is protected by International Intellectual Property Rights laws and may not be reproduced or redistributed in full or part, without the prior written consent of the author. Unauthorized reproduction of this tutorial or its contents may result in prosecution. I've worked hard on this tutorial, please don't steal it. |
Spread The Word
Related Articles
4 Responses to "Delaying Actions" 
|
said this on 17 Jun 2007 4:11:28 PM CST
the source file doesn't work in cs3
|
|
said this on 21 Aug 2007 9:02:39 AM CST
setInterval() and clearInterval() are much more elegant than this method. set interval is independent of the timeline and is actually based on time rather than the framerate.
ie... //first we set a variable that will be the max number of seconds to wait before the timer is does it's thing var maxNum:Number = 10; function myDelay(){ //i will be the counter. When i is equal to or greater than 10 your actions will execute. i=-1; if(++i=>maxNum){ //clear the startTimer to stop the loop clearInterval(startTimer) //run your actions. } } //start timer will run once every second (1000) startTimer = setInterval(this,"myDelay()",1000) |
|
said this on 24 Jan 2008 6:29:54 AM CST
error in the function...
should be >= not => |
|
said this on 17 Oct 2008 10:24:15 AM CST
Hi,
I know the setInterval method which is applied to Function. BUT I want a delay in a loop. For example. for (i = 30; i>0; i--) { trace ("test"); //delay(1000); } right now it generates test messages all at once. Obviously this //delay should be replaced with some logical command. Kindly help. |




Author/Admin)