0

I'm trying to dynamically change the text on the main stage from a sub class, but I cannot figure out how to do it. I can change the field text from the main class just fine by using myTextArea.text = "Blarg", but I'm stumped for doing it from a sub class and google hasn't helped.

My application structure is similar to:

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

    }

}

I'm sure the solution is simple, but I just can't wrap my head around it and I would love your help!

4

1 に答える 1

2

When I create classes that need a link to the main timeline, I'll pass the scope as an argument in the constructor. Something like this:

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

  }

}

Then from your main class:

var _otherClass = new OtherClass(this);
于 2012-11-06T21:53:44.777 に答える