2017-09-20 157 views
1

我看到很多問題,詢問如何在導致主時間軸中的幀的影片剪輯中製作按鈕,然後看看它們。但是,如果你想在一組特定的幀中隨機選擇幀,它會不同嗎?除了像stop()這樣簡單的東西外,我從來沒有真正使用過AS3。或gotoAndPlay。製作一個按鈕,轉到主時間軸上的隨機幀(來自陣列)。如何從影片剪輯中完成?

這裏是我的主時間軸看起來像此刻: Figure 1

下面是我用谷歌這麼遠代碼:當我用它

var frameB:Array=[1,28,45,56,71,91,106,126]; 
blue_circle1.addEventListener(MouseEvent.CLICK, choose); 

function choose1(event:MouseEvent):void { 
    var randomFrame:Number = frameB[Math.floor(Math.random() * frameB.length)]; 
    trace(randomFrame); 
    gotoAndPlay(randomFrame); 
} 

的代碼工作正常主時間軸中的旋轉按鈕。但是,當我將它放入影片剪輯中的按鈕時,它不起作用。我需要改變它,所以它的工作。如果有更好的方法做到這一點,我願意嘗試。

編輯:我應該澄清更多的東西。我從左到右移動了輪子上的矩形。它在影片剪輯中執行此操作。我想讓按鈕隨之移動。但是當我將按鈕放入所述影片剪輯時,按鈕上的代碼停止工作。我希望我不會讓事情變得更加混亂。

回答

1

當您將按鈕和/或代碼放入movieClip中時,它會更改gotoAndPlay()所引用的movieClip。您需要指定您正在調用gotoAndPlay()的哪個movieClip。對於主時間軸的gotoAndPlay()的作品,而是一個MovieClip裏面你必須使用此:

parent.gotoAndPlay(randomFrame); 

或者你可能需要父母的類型設置爲這樣的影片剪輯:

MovieClip(parent).gotoAndPlay(randomFrame); 

然而,這是最好的使用外部.as文件,因爲它使您可以最大限度地控制代碼。

  • 保存下面的代碼在一個名爲「MyFlashAnimation.as」
  • 創建一個名爲「mycodefolder」文件夾,並把它放在同一個目錄/文件夾作爲你的根。
  • 使用'MyFlashAnimation.as'作爲文檔類。在Flash /動畫IDE找到屬性面板,然後發佈部分
  • 在發佈部分地方說,「類」輸入:mycodefolder.MyFlashAnimation(不要加。至於)

CODE:

package mycodefolder { 

    import flash.display.MovieClip; 
    import flash.events.MouseEvent; 

    public class MyFlashAnimation { 

     private var animationClip:MovieClip; 
     private var blueCircle1:Button; 

     private var frameB:Array = [1,28,45,56,71,91,106,126]; 

     // constructor 
     public function MyFlashAnimation() { 

      // this your main robot/car animation 
      // this assumes animation_clip is on the main stage 
      animationClip = this.animation_clip; 

      // this is your button. this assumes blue_circle1 is a child of 
      // your animation_clip. update the path if necessary. 
      // for example, it might be: animationClip.robot_body.blue_circle1 
      blueCircle1 = animationClip.blue_circle1; 

      // add listener 
      blue_circle1.addEventListener(MouseEvent.CLICK, choose); 
     } 

     function choose1(event:MouseEvent):void { 
      var randomFrame:Number = frameB[Math.floor(Math.random() * frameB.length)]; 
      trace(randomFrame); 

      // tell animation clip to gotoAndPlay 
      animationClip.gotoAndPlay(randomFrame); 
     } 

    } 
} 

這應該工作。如果你有所有的影片剪輯路徑正確。

0

「的代碼,當我使用它的主時間軸旋轉按鈕正常工作。但是,當我把它放在一個按鈕,在 影片剪輯這是行不通的。」

將按鈕粘貼到MClip的實例名稱是什麼?該MC名稱將「添加」到按鈕的最終路徑。

例子:

(1)如果您在舞臺上blue_circle1(你已經可以做到這一點):

blue_circle1.addEventListener(MouseEvent.CLICK, choose); 

與...

2)如果您blue_circle1在另一個MClip內(例如:thingMC):

thingMC.blue_circle1.addEventListener(MouseEvent.CLICK, choose); 
相關問題