2012-11-06 100 views
0

我試圖從一個子類動態更改主舞臺上的文本,但我無法弄清楚如何去做。我可以通過使用myTextArea.text =「Blarg」來改變主類中的字段文本,但是我很難從子類中做到這一點,谷歌沒有幫助。從子類Flash AS3動態更改文本字段?

我的應用程序結構類似於:

//Main class file 
package { 

    import flash.display.Sprite; 
    import flash.events.Event; 
    import flash.display.Stage; 

    public class Main extends Sprite { 

     static public var mainStage:Stage; 

     public function Main() { 
      if (stage) stageLoader(); 
      else addEventListener(Event.ADDED_TO_STAGE, stageLoader); 
     } 

     private function stageLoader() { 
      mainStage = this.stage; 
      removeEventListener(Event.ADDED_TO_STAGE, stageLoader); 

      //This is working just fine 
      myTextArea.text = "Blarg"; 
     } 

    } 

} 

//Sub class 
package { 

    import flash.display.Sprite; 

    public class OtherClass extends Sprite { 

     public function OtherClass() { 
      //This throws "Access of undefined property myTextArea" error 
      myTextArea.text = "Blarg"; 
     } 

    } 

} 

我敢肯定,解決方法很簡單,但我不能環繞它我的頭,我會愛你的幫助!

回答

2

當我創建需要鏈接到主時間軸的類時,我會將範圍作爲參數傳遞給構造函數。事情是這樣的:

package { 

    import flash.display.Sprite; 

    public class OtherClass extends Sprite { 

     public function OtherClass(_path) { 
      //This throws "Access of undefined property myTextArea" error 
     _path.myTextArea.text = "Blarg"; 
     } 

    } 

}

從主類

然後:

var _otherClass = new OtherClass(this); 
+0

一個不錯的,簡單的解決我的問題。非常感謝你! :) – user440876