【问题标题】:How to make a universal pause button in Flash CS5?如何在 Flash CS5 中制作通用暂停按钮?
【发布时间】:2012-06-16 20:44:03
【问题描述】:

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

【问题讨论】:

标签: flash actionscript


【解决方案1】:

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

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();
        }
    }
}
}

【讨论】:

    【解决方案2】:

    此函数将停止对象的所有嵌套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);
            }
        }
    }
    

    【讨论】:

    • 请注意,如果您有不在显示列表中的电影剪辑,它们将不受此方法的影响。
    【解决方案3】:

    以下应该可以解决问题:

    // 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 = [];
    }
    

    【讨论】:

    • 太棒了,谢谢。回到我所有的电影剪辑并将每个剪辑单独添加到播放数组中有点乏味,但它确实有效。但是,清除数组必须在 for 循环之外进行,否则只会暂停数组的第一个对象。不过谢谢,非常有帮助。
    • 好地方!这就是为什么你应该总是测试孩子!更新了示例。
    • 实际上,当我一次运行多个影片剪辑(大约 5 个)时,我得到一个输出:“TypeError: Error #1010: A term is undefined and has no properties。”并不是所有的剪辑都会暂停。它还在暂停功能中表示。有什么想法吗?
    • 听起来您添加到数组中的某些影片剪辑引用是未定义的。在将每个值添加到数组之前尝试跟踪每个值并查看输出。我的猜测是,没有暂停的剪辑在您将它们添加到数组时都是未定义的。
    • 好吧,如果我正在运行说电影 A 并且只有电影 A,它将暂停。但是,有时当我运行电影 B,然后是 C,然后是 A,A 不会暂停。会不会是这个问题?
    【解决方案4】:

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

    如果您在全局可访问对象中保留了对要暂停的影片剪辑的引用,则可以遍历调用暂停的这些引用。

    【讨论】:

    • 我对 Flash 还是比较陌生,你能给我举个例子来说明你在说什么吗?感谢您的快速回复。
    猜你喜欢
    • 2011-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-09
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多