如何在Flash CS5中制作通用暂停按钮?

如何在Flash CS5中制作通用暂停按钮?,flash,actionscript,Flash,Actionscript,我试图在Flash中制作一个按钮,用于暂停文件中运行的所有电影剪辑。在我的主要时间线中,这些电影剪辑都不是介于两者之间的,它们都有各自的时间线。每个移动剪辑都由一个按钮触发,该按钮告诉剪辑开始播放。所以,如果有人能帮我创建这个暂停按钮,我将不胜感激。谢谢您抽出时间。据我所知,没有一种内置的方法可以暂停所有电影剪辑 如果在全局可访问对象中保留对要暂停的电影剪辑的引用,则可以通过调用pause来迭代这些引用 以下几点可以做到: // create an array to store all play

我试图在Flash中制作一个按钮,用于暂停文件中运行的所有电影剪辑。在我的主要时间线中,这些电影剪辑都不是介于两者之间的,它们都有各自的时间线。每个移动剪辑都由一个按钮触发,该按钮告诉剪辑开始播放。所以,如果有人能帮我创建这个暂停按钮,我将不胜感激。谢谢您抽出时间。

据我所知,没有一种内置的方法可以暂停所有电影剪辑


如果在全局可访问对象中保留对要暂停的电影剪辑的引用,则可以通过调用pause来迭代这些引用

以下几点可以做到:

// create an array to store all playing movieclips 
var playing = [];

// when a movieclip is played add it to the array like this:
// playing.push(myMovieClip);

// call this from your pause button's click handler
function pauseAll() 
{
    // loop through all the playing movieclips ...
    for (var i = 0; i < playing.length; i ++)
    {
        // ... and stop them
        playing[i].stop();
    }

    // now clear the array
    playing = [];
}
//创建一个数组来存储所有正在播放的电影片段
var=[];
//播放电影剪辑时,将其添加到阵列中,如下所示:
//播放。推(myMovieClip);
//从“暂停”按钮的“单击”处理程序调用此选项
函数pauseAll()
{
//循环播放所有正在播放的电影。。。
for(变量i=0;i
此函数将停止对象的所有嵌套movieclip。只需进入你的舞台或顶级展示课程,即可停止/播放所有内容。这样,您就不必跟踪向数组中添加内容的情况,而且没有开销

function recursiveStop(parentClip:DisplayObjectContainer, useStop:Boolean = true, gotoFrame:Object = null):void {
    var tmpClip:MovieClip = parentClip as MovieClip;
    if (tmpClip) {
        if (useStop) {
            (gotoFrame != null) ? tmpClip.gotoAndStop(gotoFrame) : tmpClip.stop();
        }else {
            (gotoFrame != null) ? tmpClip.gotoAndPlay(gotoFrame) : tmpClip.play();
        }
    }

    var i:int = parentClip.numChildren;
    while(i--){
        if(parentClip.getChildAt(i) is DisplayObjectContainer){
            recursiveStop(parentClip.getChildAt(i) as DisplayObjectContainer, useStop, gotoFrame);
        }
    }
}

使用这样的基类递归导出所有要暂停/恢复的符号,这样就不必遍历整个显示树:

package com.stackoverflow
{
import flash.display.MovieClip;
import flash.events.Event;

[Event(name="clipAdded", type="flash.events.Event")]
[Event(name="clipRemoved", type="flash.events.Event")]
public class BaseClip extends MovieClip
{
    protected var baseClipChildren:Array;
    protected var paused:Boolean = true;

    public function BaseClip()
    {
        super();
        baseClipChildren = new Array();
        addEventListener(Event.ADDED_TO_STAGE, onAdded);
        addEventListener("clipAdded", onClipAdded);
        addEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
        addEventListener("clipRemoved", onClipRemoved);
    }

    protected function onAdded(event:Event):void
    {
        var target:BaseClip = event.target as BaseClip;
        if(target == this) {
            dispatchEvent(new Event("clipAdded", true));
        }
    }

    protected function onClipAdded(event:Event):void
    {
        var target:BaseClip = event.target as BaseClip;
        if(target && target != this) {
            event.stopImmediatePropagation();
            baseClipChildren.push(target);
        }
    }

    protected function onRemoved(event:Event):void
    {
        var target:BaseClip = event.target as BaseClip;
        if(target == this) {
            dispatchEvent(new Event("clipRemoved", true));
        }
    }

    protected function onClipRemoved(event:Event):void
    {
        var target:BaseClip = event.target as BaseClip;
        if(target && target != this) {
            event.stopImmediatePropagation();
            baseClipChildren.splice(baseClipChildren.indexOf(target),1);
        }
    }

    public function stopAll():void {
        stop();
        for each(var clip:BaseClip in baseClipChildren) {
            clip.stopAll();
        }
    }

    public function playAll():void {
        play();
        for each(var clip:BaseClip in baseClipChildren) {
            clip.playAll();
        }
    }
}
}

首先阅读我对flash还比较陌生,你能给我举一个你所说的例子吗?谢谢你的快速回复。太棒了,谢谢。返回到我所有的电影剪辑,并将每个剪辑单独添加到播放阵列中,这有点乏味,但它可以工作。但是,清除数组必须在for循环之外,否则它只会暂停数组的第一个对象。谢谢,非常有帮助。好地方!这就是为什么你应该经常测试孩子们!更新了示例。实际上,当我一次运行多个电影剪辑(大约5个)时,我会得到一个输出:“TypeError:Error#1010:A term未定义且没有属性。”并且并非所有剪辑都会暂停。它也在暂停功能中显示它的名称。有什么想法吗?听起来你添加到数组中的一些movieclip引用是未定义的。尝试跟踪每个值,然后再将其添加到数组中并查看输出。我的猜测是,没有暂停的剪辑在您将它们添加到数组时都是未定义的。好吧,如果我运行的是说电影A,只有电影A,它将暂停。然而,有时当我运行电影B,然后是C,然后是A,A时不会暂停。这可能是问题所在吗?请注意,如果您有不在显示列表中的movieClips,它们将不受此方法的影响。