Groups | Blog | Home
all groups > flash actionscript > december 2005 >

flash actionscript : Accessing TextField Properties in a Loop


Hey, kid
12/19/2005 11:28:14 PM
So how come this doesn't work as I desire it to
// array of TextField instance names that in an ideal world would all look
similar
var theFields = new
Array("first_mc","second_mc","third_mc","youGetTheDrift_mc");
var totalFields:Number = theFields.length;
trace(totalFields);
for(var i = 0; i <= totalFields; i++)
{
trace(i);
trace(theFields)
boxStyle = new TextFormat();
boxStyle.background = true;
boxStyle.backgroundColor = 0x999999;
_root.theFields.setTextFormat(boxStyle);
}

NSurveyor
12/19/2005 11:39:34 PM
It doesn't work because theFields[ i ] is a String, not a MovieClip. There are
two ways to fix this: 1. Get rid of the quotes in theFields array (meaning each
element is a reference to a MovieClip) 2. Use the array access operator to find
your movieclip in the _root:


var theFields = new Array(first_mc,second_mc,third_mc,youGetTheDrift_mc);
//other code
theFields[ i ].setTextFormat(boxStyle);

OR

var theFields = new
Array("first_mc","second_mc","third_mc","youGetTheDrift_mc");
//other code
_root[theFields[ i ]].setTextFormat(boxStyle);

BTW,

boxStyle.backgroundColor = 0x999999;

won't work... You should be using TEXTFIELDINSTANCE.backgroundColor = 0x999999;

Now that I think about it, are "first_mc","second_mc","third_mc", and
"youGetTheDrift_mc" TextFields, or MovieClips? They should be TextFields....
NSurveyor
12/19/2005 11:40:19 PM
Hey, kid
12/20/2005 12:07:54 AM
Firstly, a mighty thank you for a speedy response - shame I don't understand a
lot of it ;o)

What I'm trying to do is run through a series of textfields on the stage and
give them the same look and feel; or colour, in layman's terms.

Spot on with the _mc means movieclip bit, that was a total bum-steer on my
part. Sorry. I actually had "first_txt", "second_txt", etc, etc but edited it
on the forum incorrectly. It is dynamic textfields I'm trying to influence via
a loop here.

Have you got a link to a good TextFields tutorial? I'm totally confused.
NSurveyor
12/20/2005 1:08:18 AM
background and backgroundColor are both properties of TextFields, you can't set
them through the TextFormat (the TextFormat handles more specic to the type:
font, size, color, etc.)

You could try something like:

var theFields = new Array(first_txt,second_txt,third_mc,youGetTheDrift_txt);
var totalFields:Number = theFields.length;
for(var i = 0; i < totalFields; i++)
{
var cTF = theFields[ i ];
boxStyle = new TextFormat();
with(boxStyle){
color = 0xFF0000;
bold = true;
}
with(cTF){
background = true;
backgroundColor = 0x999999;
}
cTF.setTextFormat(boxStyle);
}
Hey, kid
12/20/2005 10:06:08 AM
Oh, just the ticket.

So I had my knickers in a twist with TextFormat and TextFields, did I? No
surprises there then.

Interesting construct there with the 'with'. I've not encountered that one
before. Might have to go look up and play with/break things!

Many thanks.
AddThis Social Bookmark Button