Richard M.
Richard M.

Reputation: 29

How can I change the font color of a dynamic text field using AS3?

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

Upvotes: 2

Views: 3305

Answers (3)

Olin Kirkland
Olin Kirkland

Reputation: 545

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

Upvotes: 0

akmozo
akmozo

Reputation: 9839

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.

Upvotes: 1

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

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

Upvotes: 4

Related Questions