AS3_Manual和自动按钮事件处理程序进行故障排除

时间:2012-12-13 16:39:43

标签: actionscript-3

我有一个项目,每帧有30-40帧动画片段。将每个帧视为幻灯片。舞台底部有按钮可控制这些幻灯片的功能(例如播放,暂停,下一个,上一个,加速,减速和刷新)。然而,令我头疼的两个按钮是手动模式和自动模式按钮。他们的名字正是他们的声音,每个mc在每个帧中播放后手动应该停止。自动应按顺序播放每个帧。我现在设置它的方式是每个mc触发一个“已完成”和“已停止”的EventDispatch。根据最后点击的按钮,舞台将监听事件并暂停4秒(setInterval)和gotoNextFrame,或者在该帧停止,直到用户点击下一个。我尝试了一个switch case,但它不允许每个case中的eventlisteners错误#1009。这是我到目前为止按钮的代码......

我相当新手所以要温柔。我会上传flv,如果它会帮助更多。非常感谢你。

//Manual button actions
function manual_onClick(event:MouseEvent)
{
    manual_btn.visible = false;
    auto_btn.visible = true;
    gotoAndStop(currentFrame);
    stage.removeEventListener("finished", mcFinished);
    stage.addEventListener("stopped",stopmc,false,0);


    function stopmc(e:Event):void
    {
        trace("mc stop");
        stage.removeEventListener("stopped",stopmc);
    }
}


//Auto button actions
function auto_onClick(event:MouseEvent)
{
    gotoAndStop(currentFrame + 1);
    manual_btn.visible = true;
    auto_btn.visible = false;
    stage.addEventListener("finished", mcFinished2,false,1);


    function mcFinished2(e:Event):void
    {
        var ID2 = setInterval(goNextFrame2,3000);
        trace("mc complete");
        function goNextFrame2()
        {
            gotoAndStop( currentFrame + 1 );
            clearInterval( ID2 );
            stage.removeEventListener("finished", mcFinished2);
        }
    }
}

和每个mc的dispatchEvent

stop();

dispatchEvent(new Event("finished", true));
dispatchEvent(new Event("stopped", true));

再次感谢! 斯科特

以下是切换案例尝试......

function onBtnClicked(evt:MouseEvent):void
{
    var theBtn:DisplayObject = evt.currentTarget as DisplayObject;
    var lastBtn:DisplayObject;


    if (lastBtn)
    {
        lastBtn.addEventListener(MouseEvent.CLICK, onBtnClicked);
    }

    lastBtn = theBtn;

    switch (theBtn)
    {
        case auto_btn :
            //button one actions;
            gotoAndStop(currentFrame + 1);
            manual_btn.visible = true;
            auto_btn.visible = false;
            trace("auto button clicked");

            stage.addEventListener("finished", mcFinished);
            function mcFinished(e:Event):void
            {
                var ID = new setInterval(goNextFrame,3000);
                trace("mc complete");
                function goNextFrame()
                {
                    gotoAndStop( currentFrame + 1 );
                    clearInterval( ID );
                    stage.removeEventListener("finished", mcFinished);
                    stage.addEventListener("finished", mcFinished);
                }
            }


            break;
        case manual_btn :

            manual_btn.visible = false;
            auto_btn.visible = true;
            trace("manual button clicked");

            stage.removeEventListener("finished", mcFinished);
            stage.addEventListener("stopped",stopmc,false,1);


            function stopmc(e:Event):void
            {
                trace("mc stop");
                stage.addEventListener("stopped",stopmc);
                stage.removeEventListener("stopped",stopmc);
            }

            break;
    }
}

1 个答案:

答案 0 :(得分:0)

以下是一些建议。

首先,在每个剪辑的末尾不需要两个单独的事件(“已完成/已停止”),因为它们提供完全相同的功能。我建议只做一个事件并使用像Event.COMPLETE这样的内置事件。所以你单个剪辑的最后一帧看起来像这样:

stop();
dispatchEvent(new Event(Event.COMPLETE, true));

在各个剪辑的第一帧,我会添加:

//use these two listeners so that the clip is only listening when it's being displayed on the timeline.
this.addEventListener(Event.ADDED_TO_STAGE,init,false,0,true);
this.addEventListener(Event.REMOVED_FROM_STAGE,unload,false,0,true);

function init(e:Event){
    stage.addEventListener("Play",playMe,false,0,true);
    stage.addEventListener("Pause",pauseMe,false,0,true);
}

function unload(e:Event){
    stage.removeEventListener("Play",playMe,false);
    stage.removeEventListener("Pause",pauseMe,false);
}

function playMe(e:Event){
    play();
}

function pauseMe(e:Event){
    stop();
}

以下是我将如何处理其余代码:

stop();  //stop the main timeline

manual_btn.addEventListener(MouseEvent.CLICK, onBtnClicked);
auto_btn.addEventListener(MouseEvent.CLICK, onBtnClicked);

var nextTimer:Timer = new Timer(4000,1); //a timer to wait 4 seconds before auto next.
nextTimer.addEventListener(TimerEvent.TIMER, nextSlide); //what the timer should do when it's done

var isAuto:Boolean = true;  //a variable that holds the state of automatically continuing to the next slide. set it's default to either true or false

this.addEventListener(Event.COMPLETE,slideComplete); //listens for any slide complete event

//this function will get run whenever one of your slides is complete.
function slideComplete(e:Event = null):void {
    if(isAuto){
       nextTimer.reset();
       nextTimer.start(); //start the timer
    }
}

function onBtnClicked(evt:MouseEvent):void {
    manual_btn.visible = (evt.currentTarget == manual_btn);
    auto_btn.visible = (evt.currentTarget == auto_btn);

    isAuto = auto_btn.visible;
}

function nextSlide(e:Event = null):void {  //default e to null so you can call this function directly nextSlide(); - have your next button click call this function too
    nextTimer.reset(); //stop/reset the timer 
    nextFrame(); //tell the main timeline to go to the next frame
}

function prevSlide(e:Event = null):void {
    nextTimer.reset(); //reset the timer in case it's running
    prevFrame();
}

function pause(e:Event = null):void {
    nextTimer.reset():
    stage.dispatchEvent(new Event("Pause")); //have your individual clips listen for this event and stop
}

function playSlide(e:Event = null):void {
    nextTimer.reset();
    stage.dispatchEvent(new Event("Play")); //have your individual clips listen for this event and play;
}

您希望在所有按钮单击时重置计时器的原因是,如果您在自动下一个等待触发时在该4秒窗口中点击上一个或下一个。例如:自动下一个打开,幻灯片完成后1秒,你点击上一个按钮 - 它移动到上一个幻灯片,3秒后计时器将触发,它将移动到下一帧。