【问题标题】:AS3 addChild after set timeAS3 addChild 设置时间后
【发布时间】:2014-06-23 02:00:54
【问题描述】:

对于我们正在创建的游戏,我们需要在一定时间后“弹出”一个影片剪辑,通常在 10 到 20 秒之间。如果出现此影片剪辑,则需要在影片剪辑处于活动状态时暂停计时器,并在影片剪辑消失后重新启动计时器。有人知道怎么做吗?

【问题讨论】:

    标签: actionscript-3 timer addchild


    【解决方案1】:
    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))。

    【讨论】:

    • 我必须将 _timer 放入一个函数中才能使其工作,但它工作得很好,非常感谢! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-07
    • 2012-05-11
    • 1970-01-01
    • 2011-10-15
    相关资源
    最近更新 更多