1

我是新手Flash,我AS3正在Flash CS4Adobe Air. 我想使用按钮来播放和暂停所有内容(movieclipsAND sound)。

我已经创建了通过在playpause之间切换帧速率来工作的按钮020不幸的是,我意识到它从来都不是真正的0 fps,而是0.01 fps,所以它继续播放,尽管非常非常慢。这也不会影响音频;我已将声音文件设置为 Stream,这些文件在其时间线上同步movieclips,但即使应用程序设置为0 fps音频也会继续。

由于我有不同的关卡,movieclips每个关卡都带有同步的音频,我想做的是能够movieclip用一个按钮完全暂停每个关卡中的所有内容,并能够使用另一个按钮从同一个地方恢复。这可能吗?如果没有,是否有全局函数到pause/resume整个application/ program

4

3 回答 3

2

将侦听器添加到所有 MovieClip 实例的另一种解决方案是使用递归来暂停和恢复 MovieClip 及其所有子项(以及子项的子项,等等):

/**
 * Recursively pause a display object and all contained display objects
 * @param   clip    
 * @param   rewind  whether to go to the first frame
 */
public function recursivePause(clip:DisplayObject, rewind:Boolean = false):void 
{
    if (clip is MovieClip)
    {
        if (rewind) 
        {
            (clip as MovieClip).gotoAndStop(1);
        }
        else
        {
            (clip as MovieClip).stop();
        }
    }

    if (clip is DisplayObjectContainer)
    {
        for (var i:uint = 0; i < (clip as DisplayObjectContainer).numChildren; i++) 
        {
            recursivePause((clip as DisplayObjectContainer).getChildAt(i), rewind);
        }
    }
}

/**
 * Recursively play a display object and all contained display objects
 * @param   clip
 * @param   rewind  whether to go to the first frame
 */
public function recursivePlay(clip:DisplayObject, rewind:Boolean = false):void
{
    if (clip is MovieClip)
    {
        if (rewind)
        {
            (clip as MovieClip).gotoAndPlay(1);
        }
        else
        {
            (clip as MovieClip).play();
        }
    }

    if (clip is DisplayObjectContainer)
    {
        for (var i:uint = 0; i < (clip as DisplayObjectContainer).numChildren; i++) 
        {
            recursivePlay((clip as DisplayObjectContainer).getChildAt(i), rewind);
        }
    }
}

用法:recursivePause(stage)recursivePause(myMainMovieClip)

这比事件方法稍微方便一些,因为如果/当您将新的 MovieClip 添加到混音中时,它将“开箱即用”。

于 2013-07-01T13:39:24.843 回答
0

将帧速率设置为 0 不会停止所有动画剪辑。这样做的唯一方法是停止每个单独的影片剪辑和声音。一种方法是使用自定义事件系统:

首先,在单击停止/开始按钮时调度一个自定义事件(从舞台调度,以使其易于访问。(停止的电影剪辑的时间轴上的声音也将停止。如果您从脚本播放声音(SoundChannel ),那么你也应该阻止它们。

btnStop.addEventListener(MouseEvent.CLICK, onClick_stop);
function onClick_stop(e:MouseEvent):void {
    stage.dispatchEvent(new Event("STOP_EVERYTHING"));
}

btnStart.addEventListener(MouseEvent.CLICK, onClick_start);
function onClick_start(e:MouseEvent):void {
    stage.dispatchEvent(new Event("START_EVERYTHING"));
}

然后,在您要停止的每个影片剪辑上,收听事件:

stage.addEventListener("START_EVERYTHING", onStart_everything);
function onStart_everything(e:Event):void {
    //play this movieclip
    play();
}

stage.addEventListener("STOP_EVERYTHING", onStop_everything);
function onStop_everything(e:Event):void {
    //stop this movieclip
    stop();
}
于 2013-07-01T12:09:50.313 回答
0

我的解决方案

private var pausedArr:Array = [];

// recursive function to store all MovieClip children and their currentFrame values
public function pause(mc:*, rootObj:Boolean = false):void
{
    if (!mc is MovieClip) return;

    if (rootObj) {
        pausedArr = [];
        mc.addEventListener(Event.ENTER_FRAME, stepOneFrame);
    }

    pausedArr.push( { mc:mc, frame:mc.currentFrame, paused: false } );

    if (mc.numChildren > 0) {
        for (var i:int = mc.numChildren - 1; i >= 0; i--) {
            if (mc.getChildAt(i) is MovieClip) pause(mc.getChildAt(i));
        }
    }
}

// resume only paused MovieClips
public function resume():void 
{
    for (var i:int = 0; i < pausedArr.length; i++) {
        if (pausedArr[i].mc && pausedArr[i].paused) pausedArr[i].mc.play();
    }

    pausedArr = [];
}

// see if MovieClips' frame changed, this is to avoid resuming stopped MovieClips before pause
private function stepOneFrame(e:Event):void 
{
    pausedArr[0].removeEventListener(Event.ENTER_FRAME, stepOneFrame);
    for (var i:int = 0; i < pausedArr.length; i++) {
        if (pausedArr[i].mc.currentFrame != pausedArr[i].frame) {
            pausedArr[i].mc.stop();
            pausedArr[i].paused = true;
        }
    }
}

用于com.reintroducing.sound.SoundManager播放/暂停声音。用于com.greensock.TweenMax计时器和补间,它可以暂停/恢复两者。

于 2013-11-01T08:36:27.163 回答