簡體   English   中英

如何在另一幀中停止基於AS3計時器類的動畫?

[英]How to stop AS3 timer class-based animation in another frame?

我有一個包含4幀(+ 1個起始幀)的項目,其中每個幀都包含一個基於Timer類的動畫。

您可以通過選擇適當的按鈕來訪問每一幀。

假設第1幀中的動畫持續了30秒,但我實在太耐心了,無法觀看它並決定轉到第2幀,這也是我不喜歡的。 但是,在后台,frame1的動畫仍在運行,因為當我轉到第2幀並返回到frame1時,該動畫既從頭開始,又從我上次中斷的地方繼續播放。

我也得到一個錯誤:

TypeError: Error #1009: Kan geen eigenschap of methode benaderen via een verwijzing naar een object dat null is.
at fl.transitions::TransitionManager/saveContentAppearance()
at fl.transitions::TransitionManager/set content()
at fl.transitions::TransitionManager()
at MethodInfo-111()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()

我的問題有解決方案嗎?

當我離開一幀時如何停止動畫,以便當我再次訪問它時,它從頭開始而不是從一半開始?

感謝您的閱讀!

這是我的代碼:

陣列按鈕

var knopArray:Array = new Array(knop1, knop2, knop3, knop4);

數組功能

var functionArray:Array = new Array(naarFrame1, naarFrame2, naarFrame3, naarFrame4);


連接事件監聽器

for(var i:int = new int (0) ; i < knopArray.length; i++){
        knopArray[i].buttonMode = true;
        knopArray[i].addEventListener(MouseEvent.CLICK, klik)   
        function klik(event:MouseEvent):void{
            for(var i:int = new int (0) ; i < knopArray.length; i++)
                if (event.currentTarget == knopArray[i]){
                    knopArray[i].addFrameScript(0, functionArray[i]);
                    }
                }
        }

轉到幀並開始補間

function naarFrame1(){
        gotoAndStop("frame1");
        var timerEenA:Timer = new Timer(1000,1);
            //animations
        timerEenA.start();
        }

function naarFrame2(){
        gotoAndStop("frame2");
        var timerTweeA:Timer = new Timer(1000,1);
            //animations
        timerTweeA.start();
        }

在稱為“ init”(或類似名稱)的第一幀上,將沒有動畫:

var buttons:Array = [ button1, button2, button3, button4 ];
var frames:Array = [ "frame1", "frame2", "frame3", "frame4" ];

for each (var button:MovieClip in buttons)
{
    button.buttonMode = true;
    button.mouseEnabled = true;
    button.addEventListener(MouseEvent.CLICK, clickListener);
}

function clickListener(event:MouseEvent):void
{
    for (var i:int = 0; i < buttons.length; i++)
    {
        if (event.target == buttons[i])
            gotoAndStop(frames[i]);
    }
}

在第二個框架上,稱為“ frame1”:

// This will give you a 20 second animation, with 1 second updates.
var timer1:Timer = new Timer(1000, 20);
timer1.addEventListener(TimerEvent.TIMER, timer1Listener);
timer1.start();

function timer1Listener(event:TimerEvent):void
{
    // do something
}

在第三個框架上,稱為“ frame2”:

// This will also give you a 20 second animation, with 100ms updates.
var timer2:Timer = new Timer(100, 200);
timer2.addEventListener(TimerEvent.TIMER, timer2Listener);
timer2.start();

function timer2Listener(event:TimerEvent):void
{
    // do something
}

等等...可以使用對象更干凈地完成此操作,但這應該達到您想要的效果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM