簡體   English   中英

AS3:將鍵盤控制傳遞給子動畫片段並返回主時間軸

[英]AS3: pass Keyboard control to child movieclip and back to main timeline

編輯:我不知道這是否是規范,但我更願意保留原始問題並添加更新。 請隨意告訴我是否應該刪除原始代碼段等。

我正在嘗試使用主時間軸在Flash CS6中創建一個類似於幻燈片的演示文稿,該時間軸在每個幀中都有一個符號,並且這些符號中有不同的動畫(有些非常復雜)。 由於我要使用演示者遙控器,因此我捕獲了擊鍵,並將pg_up和pg_down編碼為分別轉到下一幀和上一幀:

stage.addEventListener(KeyboardEvent.KEY_DOWN, pagerFunction);
var symb:movieClip;
function pagerFunction(e:KeyboardEvent):void
{
    var myKey = e.keyCode;
    if (myKey == Keyboard.PAGE_DOWN){
        if (symb != null){
            //some code that allows to control the symbols timeline forward
        } else {
            nextFrame();
        }
    }
    if (myKey == Keyboard.PAGE_UP){
        if (symb != null){
            //some code that allows to control the symbols timeline backward
        } else {
            prevFrame();
        }
}

我遇到的問題如下。 我添加了framelabels和stop(); 我需要控制符號動畫中的代碼,以控制從一個動畫到下一個動畫的步驟。 但是,在Web上嘗試了多種解決方案之后,我無法成功使符號對pg_up和pg_down做出反應,就好像它們是主時間軸的一部分一樣。

總結起來,我需要解決的是:

  1. 輸入主時間軸框架
  2. 識別符號實例(標記為_mc)
  3. 在符號時間軸內,從第一幀(標記為“ 0”)播放到下一個標記幀(“ 1”,依此類推)
  4. 停止並等待下一個pg_down從下一個標記的幀開始播放到下一個(即'1'-'2'),或者等待pg_up從上一個標記的幀開始播放(即'0'到'1')(為此) ,我將使用變量進行跟蹤。
  5. 在最后一幀(標記為“最終”)上,退出符號焦點並將鍵盤控制返回到主時間軸,以允許pg_down和pg_up移至下一幀/上一幀。 在symbol.currentFrame == 0的pg_up上,執行相同的操作。

順便說一句,如果有更好的方法可以做到這一點,我很樂意(並且非常絕望)尋求更好的建議/解決方案。

非常感謝任何可以提供幫助的人!

編輯:好的,我想我對此問題不太清楚,所以我將嘗試在此添加一些內容:

addEventListener(KeyboardEvent.KEY_DOWN, mc_pagerFunction);
var lbl:String;
var counter:Number = 0;

function mc_pagerFunction(e:KeyboardEvent):void {
    var myKey = e.keyCode;
    if (myKey == Keyboard.PAGE_DOWN){
        lbl = this.currentFrameLabel;
        if (this.currentFrameLabel == 'final'){
            stop();
            stage.focus = this.parent; //which would be the main timeline
        } else if (Number(lbl) == counter){
            this.gotoAndStop(lbl);
            counter++;
        } else {
            this.gotoAndPlay(lbl);
        }
    }

    if (myKey == Keyboard.PAGE_UP){
        lbl = this.currentFrameLabel;
        if (this.currentFrameLabel == '0'){
            stop();
            stage.focus = this.parent; //which would be the main timeline
        } else if (Number(lbl) == counter){
            this.gotoAndStop(lbl);
            counter--;
        } else {
            this.gotoAndPlay(lbl);
        }
    }
}

現在,這是我要在主時間軸進入下一幀時在符號內部看到的行為,從而能夠將主時間軸用作幻燈片,並在符號內部發生真實的事情。

順便說一句,我想嘗試將所有代碼保留在主要操作層中,而不是在符號中。 我嘗試了一下,將焦點轉移到了符號上,但是它也沒有用,並且到處都有代碼使我不安;)。

我希望這能為我所堅持的觀點提供一些啟示。

再次感謝您的幫助。 提前謝謝大家

更新:請有人在這里幫助我! 這就是我正在嘗試的。 從邏輯上講,它是行之有效的,除了它不起作用。

var symb:MovieClip;

symb = MovieClip(root); //assign symbol I want to be controlled by pg_up/pg_down

symb.focusRect = false;
stage.focus = symb; //focus on current symbol

symb.addEventListener(KeyboardEvent.KEY_DOWN, mc_pager);  //add keyboard event listener

function mc_pager(e:KeyboardEvent):void{
    var myKey = e.keyCode;
    if (myKey == Keyboard.PAGE_DOWN){
        do{
            symb.play(); // it plays, then checks if the lbl is null or final, then quits
        } while (symb.currentFrameLabel == null && symb.currentFrameLabel != 'final');
        symb.stop();
        symb.removeEventListener(KeyboardEvent.KEY_DOWN, mc_pager); 
        stage.focus=MovieClip(root); //return focus to main timeline (in the next keyframes, the focus is on the nested _mc
    }
    if (myKey == Keyboard.PAGE_UP){
        do{
            symb.prevFrame();
        } while (symb.currentFrameLabel == null && symb.currentFrameLabel != '0');
        symb.stop();
        symb.removeEventListener(KeyboardEvent.KEY_DOWN, mc_pager); 
        stage.focus=MovieClip(root);
    }
}

我要去moronic把它弄到哪里去? 拜托,各位,您是專家,我在這里需要您的建議。 謝謝!

更新:在symb上進行跟蹤時,似乎一進入函數,它就會忘記初始分配(symb = MovieClip(root))並顯示null。 為什么?

在此示例中,我創建了一個基本的概念證明,其中在主時間軸上有一個實例名稱為“ c”的圓圈MC。 在此mc中,我在內部創建了2個關鍵幀,並在兩個關鍵幀上都設置了停止操作。 我在第2幀上為圓圈塗了另一種顏色,並將其標記為“兩個”。

在主時間軸中,我有以下代碼:

import flash.events.KeyboardEvent;

stage.addEventListener(KeyboardEvent.KEY_DOWN,keyEvt);

function keyEvt(e:KeyboardEvent):void{
        if(e.keyCode == Keyboard.PAGE_DOWN){
                trace("down");
                c.gotoAndPlay("two");
            }
    }

只要您堅持通過直接引用來定位符號,並確保將鍵盤事件附加到舞台上,這將有助於為您的代碼奠定基礎。

另外,始終堅持先降低基本工作版本。 IE檢查您的鍵盤偵聽器是否正在為您的目標對象工作,然后從中構建其他功能。

暫無
暫無

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

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