Dynamically changing text in a Static TextField

February 1st, 2007 . by polyGeek

I always thought that you couldn’t change the text value for a static TextField at runtime. I was wrong, again.

You probably know that any object on the stage that doesn’t have an instance name is given one automatically when published. That also applies to static TextFields. In my situation I have a MovieClip with multiple dynamic TextFields that were going to get their text values from XML data. There are other TextFields, that act as labels, have their type set to Static Text in the properties panel.

So far this is stuff that you and I have done countless times before. No problem.

At this point I got a little cute with the code. I’m accessing the text data from an object. So for a given index I can say something like this:

publisher.text = o.publisher
rating.text = o.rating
genre.text = o.genre
….

That works well enough but what if I add another TextField/data pair then I have to go back into my code and associate them. That’s just too much work.

I set this up from the beginning so that I could use a for-in loop to run through all the TextFields in the MovieClip that holds them. That way if I add another TextField to the MovieClip and the corresponding data to the XML I’m done. (Aside: for someone who loves to code I really like to find ways to not code. Go figure.)

So now we have something like this going on:



Great, but all my labels came up as undefined. WTF?

It didn’t take long to figure out that the code was trying to give mc.instance1.text = o.instance1. Fixing it only took checking to see if there was a corresponding value in the Object.

if(mc[p].text && o[p]) {….

It looks like Static TextFields and Dynamic TextFields are really the same thing. It’s just that the authoring tool doesn’t give you the ability to provide an instance name to the Static TextFields.

Update: You can read more from the comments but basically the text in a static TextField can only be changed if the font is set to _sans, _serif, _typewriter. That’s because even though you have the TextField set as static Flash still publishes them as dynamic because they are. Those TextFields get populated at runtime depending on the fonts available on the computer that the SWF is running on.

So while you can’t give a static TextField with it’s font set as a devise font an instance name it still gets one and can be accessed in the for-in-loop.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • Digg
  • Facebook

6 Responses to “Dynamically changing text in a Static TextField”

  1. comment number 1 by: Phillip Kerman

    could you find a way to change the text in a static field? I don’t think you can. The user doesn’t need the font installed to display static text…so Flash just embeds the outlines it needs.

    Thanks,
    Phillip

  2. comment number 2 by: Greg Burch

    What Phillip said!

  3. comment number 3 by: polyGeek

    You know, I was using a font type of “_sans”. So, maybe that’s why the text value could be changed. When I get to work in the morning I’ll test that theory out and make a downloadable example.

  4. comment number 4 by: polyGeek

    I did a quick test. A static TextField that uses a device font - _sans, _serif, _typewriter - can be changed at runtime.

    If you set the font to anything else then no, you can’t change the text value of the TextField.

  5. comment number 5 by: collis

    As always, very clever!!

  6. comment number 6 by: polyGeek

    Well, that’s why I get paid the big bucks. :-)

Leave a Reply

Name

Mail (never published)

Website

- Why ask? This confirms you are a human user!

   




© Copyright 2008 polyGeek.com / Dan Florio, All Rights Reserved Except Where Explicitly Stated
Web Developement Blogs - Blog Catalog Blog Directory
M2 Websites