For a game we are creating we need to have a movieclip 'pop up' after a certain amount of time, usually somewhere between 10 and 20 seconds. If this movieclip appears, the timer needs to be paused while the movieclip is active and the timer needs to be restarted after the movieclip disappears. Anyone knows how to do this?
import flash.utils.setTimeout;
// Your Sprite / MovieClip
var clip:MovieClip;
// The time until you want to add the first one
var timeToAdd:uint = Math.random() * 20;
// This will be the timer
var _timer:uint;
// This will add us to the stage after the random time. Second variable is seconds, so we need to multiply by 1000.
_timer = setTimeout(addToStage, timeToAdd * 1000);
// Called when the timer expires
function addToStage():void{
clip = new MovieClip();
// You would need logic to decide when to remove it, but once it is removed this will fire
clip.addEventListener(Event.REMOVED_FROM_STAGE, onRemove);
}
// Called once removed
function onRemove(e:Event):void{
// Remove the event listener
clip.removeEventListener(Event.REMOVED_FROM_STAGE, onRemove);
// Restart the timer
timeToAdd = Math.random() * 20;
_timer = setTimeout(addToStage, timeToAdd * 1000);
}
The above code will add yout sprite to the stage once within 0.001 - 20 seconds. You'd need to add a bit of code to remove your sprite (removeChild(clip)).