Skip navigation
Currently Being Moderated

TextField text changed during parent's container gets reset

Aug 6, 2012 10:21 AM

Tags: #text #change #constructor #textfield

I'm having an aggravating issue which I think might be an inherent problem with Flash, but maybe I'm missing something. I'm finding that if I write a class file for a MovieClip in my library that contains a TextField, and try to change the TextField text in the constructor, it gets reset or doesn't set or something. I'm thinking maybe Flash runs the code to create the library version of the MovieClip after your custom constructor?

 

So for example right now, I've got a MovieClip in my library called "MenuBar". MenuBar has a background bitmap and a TextField with instance name lblTitle, which has the text "Title" within the library.

 

I've written a class file for MenuBar which has this function:

 

          /**The title of the menu displayed in the center of the menu bar

                     */

                    public final function set title(text:String):void {

                                   trace(lblTitle);

                                   if (lblTitle != null) {

                                                  trace(text);

                                                  lblTitle.text = title;

                                             trace(lblTitle.text);

                                   }

                    }

 

Then I've got a MovieClip in the library called "MenuScreen". It has an instance of MenuBar called menuBar. MenuScreen also has its own class file which starts like this:

 

public function MenuScreen(titleText:String)

         {

                    super();

                    menuBarTop = MenuBar(this.getChildByName("menuBar"));

                    trace(titleText);

                    trace("Old Title:" + menuBarTop.title);

                    menuBarTop.title = titleText;

                    trace("New Title:" + menuBarTop.title);

         }

 

Now say I run this code:

 

var menu:MenuScreen = new MenuScreen("New Title");

 

The debug output comes out like this:

 

New Title

Old Title:Title

TextField: [object TextField]

Incoming Text: New Title

New text: Title

New Title:Title

 

It's running the code to change the text in the TextField, and doesn't throw an error, but it doesn't change the text. What am I missing?

 
Replies
  • Currently Being Moderated
    Aug 6, 2012 10:30 AM   in reply to xTLS

    I won't pretend to follow all of what you just described, but here's an observation... In the function you show you are assigning "title" to lblText.text, but you are passing in a variable you call "text"  I don't see where title is defined anywhere.

     

                        public final function set title(text:String):void { 

                                       trace(lblTitle);

                                       if (lblTitle != null) {

                                                      trace(text);

                                                      lblTitle.text = title;

                                                 trace(lblTitle.text);

                                       }

                        }

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 6, 2012 10:51 AM   in reply to xTLS

    Gee, you are awfully touchy... interpretting what I said as being an insult towards you when it was an indication of my own limitations.  I was indicating that I do not easily follow matters that concern classes - but I thought I would point out something that caught my eye.  Sorry to have offended you by trying to help.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 6, 2012 3:09 PM   in reply to xTLS

    I usually do not try to do constructor injection on View Classes (but then again, I usually allow the Flash Player/timeline to handle the instantiation and population of all my instances). I find that when the Flash player creates timeline instances, all of their children (that are present on frame 1, of course) are accessible from within the constructor, simply by referencing the instance name.  So if you did something more like this:

     

    public var menuBarTop:MenuBar;

    protected var _title:String='default title';

     

    public function MenuScreen() {

      if (menuBarTop) {

        menuBarTop.title= 'default title';//this should work fine

      }

    }

     

    public function get title():String {

       return _title;

    }

    public function set title(value:String) {

      if (value != _title) {

        _title = value;

        if (menuBarTop) {

          menuBarTop.title = _title;

        }

    }

     

    For tighter control, you can use a getter/setter for menuBarTop as well.

     

    So, you're probably wondering how you are going to "get hold" of MenuScreen in order to populate it if you allow Flash to instantiate it? If you're not sure, check out http://www.developria.com/2010/04/combining-the-timeline-with-oo.html or http://www.meetup.com/atlflex/files/ (the files there all relate to this discussion)

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points