Frage

I have 3 movieclips and each has a textbox as a child.

I set the active one with

var myroot:MovieClip = this.root as MovieClip;
var activeText:MovieClip;

This works

function keyClicked (e:MouseEvent) {
    myroot.firstname_mc.getChildAt(0).text += "hello";
}

This does not

function keyClicked (e:MouseEvent) {
    activeText.getChildAt(0).text += "hello";
}

How can I get this to work dynamically?

War es hilfreich?

Lösung

Your whole problem is that you're trying to do things that you shouldn't do. What you should do instead is write Classes that encapsulate the desired behavior, and let them handle the details. For example:

package view {
   public class Label extends MovieClip {
      /* This is public so the Flash Player can
         populate it, not so you can "talk" to it
         from outside. This is a stage instance
      */
      public var tf:TextField;
      protected var _text:String;
      public function get text():String {
         return _text;
      }
      public var set text(value:String):void {
         if (value != _text) {
           _text = value;
           tf.text = _text;
         }
      }
   }

}

Now, in your main Document Class, you type activeText as Label, and you can then set its text like this:

activeText.text += 'hello';

And you can now reuse the new Class you wrote to make all sorts of different-looking Labels, just as long as each contains a TextField called tf.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top