如何使用AS3更改动态文本字段的字体颜色?

时间:2016-06-01 17:01:28

标签: actionscript-3 fonts

如何使用Flash中的Actionscript 3更改动态文本字段内的字体颜色?我想将其实现为if语句,如下所示:

if (randomVar == 0) {
    score.color = #0xFFFFFF;
} else if (randomVar == 1) {
    score.color = #0xFAFAFA;
} else {
    score.color = #0xAAAAAA;
}

3 个答案:

答案 0 :(得分:4)

您必须使用TextFormat。这是一个例子:

//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 :(得分:1)

更改TextField文本颜色的另一种方法是使用TextField.textColor属性:

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

希望可以提供帮助。

答案 2 :(得分:0)

根据我的经验,最好的方法是使用HTMLText。像这样:

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