• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

TextField text changed during parent's container gets reset

Contributor ,
Aug 06, 2012 Aug 06, 2012

Copy link to clipboard

Copied

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?

TOPICS
ActionScript

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Aug 06, 2012 Aug 06, 2012

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='de

...

Votes

Translate

Translate
LEGEND ,
Aug 06, 2012 Aug 06, 2012

Copy link to clipboard

Copied

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);

                                   }

                    }

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Aug 06, 2012 Aug 06, 2012

Copy link to clipboard

Copied

I won't pretend to follow all of what you just described

Are you not capable of helping people without sticking in some mean comment? I feel like you're here to feel better than everyone else, rather than to actually be helpful. Thank you for the help, anyway, that was a stupid mistake, I seem to be making a lot of those lately... I guess I should sleep more than six hours a night...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 06, 2012 Aug 06, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Aug 06, 2012 Aug 06, 2012

Copy link to clipboard

Copied

Fair enough, sorry about that, I guess I am somewhat touchy >_< . This turned out to be related to a string of misunderstandings and the typo just destroyed the whole thing.

I had found if I did:

private var lblTitle:TextField;

public function MenuBar(text:String) {

    lblTitle = TextField(this.getChildByName("lblTitle"));

    lblTitle.text = text

}

it would throw a null reference error (I guess because lblTitle hasn't been added to the stage yet? I need to look into that more) so I thought "you can't access the TextField from the constructor" and wrote the set text function and got progressively more confused from there >_<

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Aug 06, 2012 Aug 06, 2012

Copy link to clipboard

Copied

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)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Aug 08, 2012 Aug 08, 2012

Copy link to clipboard

Copied

LATEST

Thanks, I have a better understanding of what's going on behind the scenes now!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines