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

4

5

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!

Varsity answered 21/6, 2012 at 16:23 Comment(0)
V
11

Label does not have a color property, rather it has a color style which can be set like so:

lblUpgrade.setStyle("color","#000000");
Visakhapatnam answered 21/6, 2012 at 16:29 Comment(0)
M
5

Styles are accessed like this in as3

lblUpgrade.setStyle("color","#000000");
Markus answered 21/6, 2012 at 16:28 Comment(0)
T
4

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

Terminus answered 21/6, 2012 at 16:36 Comment(0)
C
2

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.

Cysticercoid answered 30/7, 2014 at 10:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.