0

我正在尝试从子类动态更改主舞台上的文本,但我不知道该怎么做。我可以通过使用 myTextArea.text = "Blarg" 很好地更改主类中的字段文本,但我很难从子类中进行更改,而谷歌也没有帮助。

我的应用程序结构类似于:

//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";
        }

    }

}

我确信解决方案很简单,但我无法理解它,我很想得到你的帮助!

4

1 回答 1

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);
于 2012-11-06T21:53:44.777 回答