Jesse lives and works in Melbourne Australia. He is the Cofounder and a Director of http://ActionScript.org. A Flash enthusiast, teacher, author, freelancer and speaker Jesse enjoys participating in the http://ActionScript.org community and the wider Flash scene when he has time.
This face will duplicate at a random point, every two seconds, until there are 20 faces, then stop.
Download the source here.
The inspiration behind this tutorial is two fold. Firstly, it's generally useful to know how to do, and secondly it will stop us having to explain how every time someone wants to perform such an action. Recently there's been a lot of requests along the lines of "I want to do <some event> every <random number of> second until <some condition is met>". Now in Flash 4 most people achieved this using very long MovieClips with lots of empty frames, or the more sophisticated used GetTimer. In Flash 5 we have all sorts of fancy controls over the time elements and I still prefer the getTimer method. Call me old fashioned if you will.
Let's get into it! There's not really much of a need for an intro to this tutorial so I'll just show you the code and explain how it works, shall I?
Define this variable in Frame 1 of your movie:
[as]_root.wait = 10;[/as]
This 'wait' variable is the number of second to wait between each event. It must be an integer greater than 1! For those of you who don't speak Math, that means a whole number, so no decimal points or fractions.
Create a controller MC (just a blank MC which will sit off the stage somewhere) and add these actions:
[as]onClipEvent (enterFrame) {
if (_root.delay) {
if (Math.floor(getTimer()/1000) == _root.lastTime+1) {
_root.delay = false;
}
} else if (Math.floor(getTimer()/1000) == _root.lastTime+_root.wait) {
_root.lastTime = Math.floor(getTimer()/1000);
_root.delay = true;
// custom actions go here
}
}[/as]
What it does (line by line):
Don't worry, I explain it a bit better over the page ...
So you can add any actions you want in at Line 9 (note you can add multiple lines of code) and this script will run that code every X seconds. You can also add an extra If to your own code with a counter variable if you only want to perform a certain action say 10 times.
Finally, this code assumes that your actions begin at the beginning of the movie. If you don't wish to start repeating actions instantaneously when the movie starts, you will need to trigger this code using a button or the like. You will also need to take into account the value of getTimer() when the user clicks the button, as the code above assumes it is zero. It's not hard to do, all you have to do is create another variable which remember at what time your user began the wait loop.
If you have any problems post your questions on the forums please and someone will answer them if they can.
Good luck!
| Jesse Stratford [email:jessestratford@actionscript.org] 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. |