2014-06-22 67 views
-1

對於我們正在創建的遊戲,我們需要在一段時間後(通常在10到20秒之間)彈出一個動畫片段。如果出現此影片剪輯,則在影片剪輯處於活動狀態時需要暫停計時器,並且在影片剪輯消失後需要重新啓動計時器。任何人都知道如何做到這一點?設置時間後的AS3 addChild

回答

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秒內將yout精靈添加到舞臺上。你需要添加一些代碼來移除你的精靈(removeChild(clip))。

+0

我不得不把_timer放在這個工作的函數中,但它很好用,非常感謝! :) –