http://www.cnblogs.com/rob0121/articles/1756942.html
好多人也应该遇到和我一样的问题,当设计师给了我们一个多层嵌套的movieClip时,我们在写代码时无法将movieClip完全停止掉,主要是基于好多movieClip有深层嵌套,主时间轴不在最上层导致的,所以基于这个原因写了下面的类。
看代码:(有很详细的注释)
复制内容到剪贴板
代码:
package com.sina.utils
{
import flash.display.MovieClip;
/**
* .主要是解决一个单帧影片剪辑无法暂停和重播的问题
* @author
[email protected]
*/
public class MovieClipManage
{
private var _target:MovieClip;
private var _type:String;
/**
* 构造函数
* @param mc 目标影片剪辑
*/
public function MovieClipManage(mc:MovieClip = null)
{
_target = mc;
}
private function goto(mc:MovieClip, frame:int = 0):void
{
if(!(mc is MovieClip))
{
throw new Error("MovieClipManage::: 目标不是movieClip");
return;
}
frame > 0 ? mc[_type](frame) : mc[_type]();
if (mc.numChildren > 0)
{
for (var i:int = 0; i < mc.numChildren; i++ )
{
if (mc.getChildAt(i) as MovieClip)
{
goto(mc.getChildAt(i) as MovieClip, frame);
}
}
}
}
//--------------------API---------------------------------
/**
* 暂停影片剪辑
*/
public function pause(target:MovieClip = null):void
{
_type = "stop";
target = target || _target;
goto(target);
}
/**
* 恢复暂停影片剪辑
*/
public function play(target:MovieClip = null):void
{
_type = "play";
target = target || _target;
goto(target);
}
/**
* 停止影片剪辑到第一帧
*/
public function stop(target:MovieClip = null):void
{
_type = "gotoAndStop";
target = target || _target;
goto(target, 1);
}
/**
* 重播影片剪辑
*/
public function replay(target:MovieClip = null):void
{
_type = "gotoAndPlay";
target = target || _target;
goto(target, 1);
}
}
}
//---------------------------------------------------------------------
实现原理:主要用到了递归的原理来循环找到底层的movieClip原件,然后执行相应的操作来达到将所有嵌套的原件暂停,重播等功能。
原文地址:http://www.99ria.com/blog/?p=230