对于我们正在制作的游戏,我们需要有一个动画片段弹出'经过一段时间后,通常在10到20秒之间。如果出现此动画片段,则在动画片段处于活动状态时需要暂停计时器,并且在动画片段消失后需要重新启动计时器。谁知道怎么做?
答案 0 :(得分:0)
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);
}
上面的代码会在0.001到20秒内将你的精灵添加到舞台上一次。你需要添加一些代码来删除你的精灵(removeChild(clip))。