2014-03-03 30 views
0

我正在玩閃光燈,並且爲菜單,按鈕等等創建了多個場景。當嘗試爲處於一個場景中的按鈕添加事件處理程序時,但不是其他人,編譯器抱怨說它不能引用不存在的對象。AS3 - 如何在課堂上獲取當前場景名稱

我想出瞭解決方案很簡單...獲取場景名稱,比賽,對if語句,並加載事件處理程序通過if語句...

然而,在網絡上週邊後挖掘太長時間了,我似乎無法找到正確做到這一點的方法。有誰知道一種方式?

我使用以下嘗試:

var scene:Scene = myflvandclassname.currentScene; 
var sName:String = MovieClip.currentScene.name; 

Both lead to an error "Access of possibly undefined property Scene through a reference with static type Class". 
+1

你試過了什麼?請包括代碼,錯誤等 – Vic

+0

大多數情況下,我一直在試圖將場景名稱分配給字符串變量。錯誤主要是「通過靜態類型類的引用訪問可能未定義的屬性場景」。我試過的東西= var sName:Scene = myflvandclassname.currentScene ;, var sName:String = MovieClip.currentScene.name,以及那種類型的東西。 – user3374014

+0

請將其編輯到問題中,以便其他人可以訪問 – Vic

回答

0

省略影片剪輯和場景作爲源組織項目和代碼在時間軸上。使用Document class作爲入口點。這tutorial應該幫助你掌握主要概念。

package { 

    import flash.display.Sprite; 
    import flash.display.StageAlign; 
    import flash.display.StageScaleMode; 
    import flash.events.Event; 

    public class StackOverflow extends Sprite { 
     public function StackOverflow() { 
      addEventListener(Event.ADDED_TO_STAGE, onAdded); 
     } 

     private function onAdded(e:Event):void { 
      removeEventListener(Event.ADDED_TO_STAGE, onAdded); 

      stage.align = StageAlign.TOP_LEFT; 
      stage.scaleMode = StageScaleMode.NO_SCALE; 

      setup(); 
     } 

     private function setup():void { 
      const padding:int = 20; 

      //Initiate your UI components and place them in the display list 
      var menu:MyMenu = new MyMenu(); 
      var buttons:MyFooterButtons = new MyFooterButtons(); 
      var etc:AnotherComponent = new AnotherComponent(); 

      addChild(menu); 
      addChild(buttons); 
      addChild(etc); 

      menu.x = menu.y = padding; 
      //Place them and initialise with concrete information 
     } 
    } 
} 

例如,的MyMenu,MyFooterButtons,AnotherComponent可能是影片剪輯/雪碧在圖書館export settings,在那裏你做了所有與佈局,造型工作等

0

我有同樣的問題。我想從外部Class中檢查當前場景名稱,並根據名稱(遊戲級別的名稱)在某些屬性中傳遞某些值...因此,我所做的和它的工作原理就是這樣。

//main in 1st frame of the fla 

stop(); 

var myCheckSceneClass: CheckSceneClass = new CheckSceneClass(); 
myCheckSceneClass.myCurrentScene = currentScene; 
myCheckSceneClass.checkScene(); 

//CheckSceneClass 


package { 
    import flash.events.MouseEvent; 
    import flash.display.MovieClip; 
    import flash.display.Scene; 


    public class CheckSceneClass extends flash.display.MovieClip { 

     public var myCurrentScene : Scene; 



     public function CheckSceneClass() { 


     } 


     public function checkScene() { 
      switch (myCurrentScene.name) { 
       case "Scene 1": 
trace("It is the 1st Scene");     
break; 
       default: 
        trace("Error with Scene Name"); 
        break; 

      } 

     } 








    } 

}