|
|||||||
| Tutorials:flash:actionscript:timed_action | ||||
|
As a young clock first meets his challenge of moving from the creation of animations to creating video games, it becomes obvious that tweening your sprites and animating movements independent of your programmatic control will turn your code into a mess in no time. What is usually needed is the ability to make events in flash happen at a certain time or in a certain sequence. Using the setInterval function, you can animate scenery and timed interactions simply and effectively. Here is the format for setInterval: intervalObject = setInterval(function,time[,parameters]); The way setInterval works is this. You create a function called function and tell flash to wait for time number of milliseconds before calling it with the optional parameters. Flash then calls this function over and over again until you tell it to stop with: clearInterval(intervalObject); Note: While you can execute code faster than the FPS of the movie, you can’t update the stage any faster than the FPS of the movie. So by assigning the intervalObject that setInterval creates to a global variable, we can clear it as soon as it executes the first time, for one-time delays, like this: function enemyHit() { //the _root.enemy is destroyed, spin it for 3 seconds and then play the explosion animation. _root.enemycall = setInterval(doSpinAndDie(),100); _root.enemydiecounter = 0; } function doSpinAnddie() { //this function gets called every 10th of a second. _root.enemydiecounter++; if (_root.enemydiecounter < 30) { _root.enemy._rotation += 10; } else { //clear the interval so we stop calling doSpinAndDie() clearInterval(_root.enemycall); _root.enemy.gotoAndPlay(2); //explode or something } } 1) tutorial not finished |