2
votes

How can I change the color of the font inside of a dynamic text field using Actionscript 3 in Flash? I'd like to implement this into an if-statement, kind of like this:

if (randomVar == 0) {
    score.color = #0xFFFFFF;
} else if (randomVar == 1) {
    score.color = #0xFAFAFA;
} else {
    score.color = #0xAAAAAA;
}
3
As an aside, if your randomVar is the product of Math.random(), it will never produce a value of 1 and very very rarely be exactly 0.BadFeelingAboutThis
That wasn't actually an example of my code. If was just to demonstrate the concept Id like to have.Richard M.

3 Answers

4
votes

You have to use a TextFormat. Here is an example:

//first, get the text format that you've applied in Flash at design time:
var txtFormat:TextFormat = score.defaultTextFormat;

//then later, in your if statement:
txtFormat.color = 0xFF0000; //red

//modifying the text format object doesn't actually trigger any changes to text fields
//you have to apply it to your text field to see the changes
score.setTextFormat(txtFormat);
1
votes

Another way to change the color of a TextField's text is to use the TextField.textColor property :

// the color here is in hexadecimal format, you don't need the "#"
score.textColor = 0xFAFAFA;    

Hope that can help.

0
votes

In my experience, the best way to do this is using HTMLText. Like this:

var color:uint = 0xffffff;

if (score > 5)
    color = 0x00ff00;
if (score > 10)
    color = 0xff0000;
if (score > 20)
    color = 0x0000ff;

myText.htmlText = "<font color='" + color + "'>" + score + "</font>";