如果我制作一个电影剪辑“.visible”它仍然在后台AS3中运行吗?

时间:2014-02-24 07:42:44

标签: actionscript-3 flashdevelop visible

我有一个MovieClip的菜单tweens一直在运行,当用户点击开始游戏时,当我们添加menu.visible = true;时,这会阻止菜单运行背景还是只是让它不可见,它会继续在后台运行并降低性能?

1 个答案:

答案 0 :(得分:1)

内存中MovieClip的所有实例(即存在对它们的引用或尚未由GC收集)正在播放并消耗一些处理器时间来触发EnterFrame事件或执行框架脚本。但是设置visible=false或将其从stage中删除以防止其呈现仍然很重要,这可能导致消耗更多资源。

小额奖金,我使用这种实用程序方法停止所有动画,然后将其从display list移除,希望它有所帮助:

/**
 * Stops all animations in the MovieClip and all its chilrend recursivly
 *
 * @param   target
 * @param   self stop animation in target or not
 * @param   isGoToAndStopFirstFrame move all clips to the first frame
 *
 */
public static function stopAll(target:DisplayObject, self:Boolean = true, isGoToAndStopFirstFrame:Boolean = false):void
{
    if (!target)
        return;

    var t:int = getTimer();
    var targetMovieClip:MovieClip = (target as MovieClip);
    if (self && targetMovieClip)
    {
        if(isGoToAndStopFirstFrame)
        {
            targetMovieClip.gotoAndStop(1);
        }else
        {
            targetMovieClip.stop();
        }
    }

    //stops all children in DisplayObjectContainer
    var targetContainer:DisplayObjectContainer = (target as DisplayObjectContainer);
    if(targetContainer)
    {
        for (var i:int=0; i<targetContainer.numChildren; i++)
        {
            var child:DisplayObject = targetContainer.getChildAt(i);
            if (child)
            {
                stopAll(child, true, isGoToAndStopFirstFrame);
            }
        }
    }

    // stops all states in SimpleButton
    var targetSimpleButton:SimpleButton = (target as SimpleButton);
    if(targetSimpleButton)
    {
        stopAll(targetSimpleButton.overState, true, isGoToAndStopFirstFrame);
        stopAll(targetSimpleButton.upState, true, isGoToAndStopFirstFrame);
        stopAll(targetSimpleButton.downState, true, isGoToAndStopFirstFrame);
        stopAll(targetSimpleButton.hitTestState, true, isGoToAndStopFirstFrame);
    }

    return;
}