- Home
- Tutorials
- Flash
- Intermediate
- Timer Controlled Events

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 StratfordNow that run-down is terribly in-depth so let me break it down a bit. You start with no variables so your delay variable is not true and your lastTime variable has no value. Therefore the delay == true If clause is skipped and the computer checks if the current time is equal to lastTime ( zero ) plus the wait variable. If it isn't it does nothing and the whole process is performed again, every frame, until the current time is equal to the specified waiting time.
Now the actions within the Else If are carried out. The lastTime variable is set to the current time, and the delay variable is set to true. Then all your custom actions (which you insert at line 9) are carried out also.
The delay variable is there because this loop is run through many times per second, and during that one second, the Else If clause is always true, thus if your frame rate is 12 fps, you would get these actions executing 12 times every time, instead of just once. So I added the delay variable which is true once the actions have been performed once. It is reset to false 1 second later. This is why your wait time must be greater than 1 second!
The lastTime variable just keeps being incremented to indicate the time when we last perform the actions. It's basically just used to keep track of how long until we need to perform the actions again.
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 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. |


