Dead simple but won't work, stupid textFormat

OK, I know this is simple. I’ve read every thread on this board pertaining to this (including senocular’s Best Of thread), done everything there and this still doesn’t work and I can’t see why.

I feel like an idiot even asking, because I’ve seen the examples and lengthy tutorials.

All I want to do, is change the color of a dynamic text field when you press a button. But I want the name of the field to be a variable.

Now, this is what’s there:

  1. the variable “_root.expinttype1” is equal to “branch” and is set elsewhere.
    1a. There is an existing dynamic field called “branch” on screen.

  2. On the button release I set the format:

expertText = new TextFormat();
expertText.color = 0xFF0000;

  1. then tell the stinking field to change.

this works --> this[“branch”].setTextFormat(expertText);
this works --> this.branch.setTextFormat(expertText);
this does not work --> this["_root.expinttype1"].setTextFormat(expertText);

Why? I set a dummy variable txt = _root.expinttype1; and even threw it into a text field to verify the data, and it shows “branch”, so why doesn’t this work?

:jail:

_root.expinttype1.setTextFormat(expertText);

oh I tried that one, believe me! I might note that the text field “branch” is in a movie clip. The _root.expinttype1 variable is getting it’s data from an XML file too.

I also tried using that temp variable “txt” like txt.setTextFormat(expertText);

Is it because your location is encapsulated in quotes?

You’re targetting a variable, but writing it as a string. Try this…

this[_root.expinttype1].setTextFormat(expertText);

Unless I misunderstood :x

aaarrrggghh!! Fer cryin’ out loud. that did it. I could swear that I tried that already.

This from senocular confused me I guess:

_root.clip1

can also be written as

_root[“clip1”]

where you can replace .name with [“name”]

Thanks guys. I knew it was something stupid. C:-)

That’s because the name is a string.

What you were doing was targetting a variable to get’s it’s value. But by surrounding it with quotes you were making flash think it was a string thus flash thought you were looking for something else.

To help try and clarify here is a bit of code that might help explain.

var myVar = "asdf";
trace(this[myVar]);

This will return undefined int he trace. This is because Flash will be looking for a clip called “asdf” in the timeline. Since you target myVar as a variable, it reads the value of the variable.

You must target the NAME of the variable for Flash to return the actual variable itself… example…

var myVar = "asdf";
trace(this["myVar"]);

This will return “asdf” in the output window.

Making it a string works because with Associative Array Referencing, all names of the objects in the timelines scope are stored as strings.

Yeah, i’m definitely horrible at explaining things. And it doesn’t help that i’m rushing. Sorry.