简体   繁体   中英

How can you change a Label's color at runtime in ActionScript 3.0?

I get the following error:

1119: Access of possibly undefined property color through a reference with static
type mx.controls:Label.

The thing about that is that, in the MXML, color is an attribute of Label. But if I try to say something like:

lblUpgrade.color = "#000000";

it throws this error. I've been trying to find a work-around for the last 45 minutes. How can I set this at runtime? Thanks!

Label没有color属性,而是具有可以这样设置的颜色样式:

lblUpgrade.setStyle("color","#000000");

在as3中可以像这样访问样式

lblUpgrade.setStyle("color","#000000");

color is a style not a property, you set it using setStyle . Also with as3 you use 0x instead of # for the color, but maybe that works for styles.

lblUpgrade.setStyle("color", "0x000000");

Wow, I've been struggling for 45 minutes AFTER I found this post. I'm using Adobe CS6 (don't ask why!) and the only way that finally works for me is this:

/* Create a new TextFormat object, 
which allows you to set multiple text properties at a time. */ 

var tf:TextFormat = new TextFormat(); 
tf.color = 0xFF0000; 

/* Apply this specific text format (red text) to the Label instance. */ 
a_label.setStyle("textFormat", tf);

Hope this helps someone. Source: Adobe Help Center

You can also use TextFormat to change other properties like Font, Size etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM