I have this very simple custom component, made out of two Labels: _left and _right.
It should represent several kinds of score in a game: 1.2.3.4.5.6
The last number ("6" in the above example) should be in bold font if it has just changed (in the current game round), otherwise all numbers should look same.
Also the last number should be red or green (depending if it's "bad" or "good" score).
Here is my source code for ScoreLabel.mxml:
<?xml version="1.0" encoding="utf-8"?>
<s:HGroup
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
gap="0">
<fx:Script>
<![CDATA[
public function setText(str:String, changed:Boolean=false):void {
if (!changed) {
_right.text = '';
_left.text = str;
return;
}
var array:Array = str.split('.');
_right.text = array.pop();
_left.text = array.join('.') + '.';
}
public function setColor(n:uint):void {
_right.setStyle('color', n);
trace('setColor: ' + n);
}
]]>
</fx:Script>
<s:Label id="_left" width="100%" textAlign="right"/>
<s:Label id="_right" width="25" fontWeight="bold" color="#006600"/>
</s:HGroup>
My problem is: when I call myLabel.setColor(0xFF0000); the text doesn't change to red, but stays in the default green color - even though I can see the traces in debugger.
Any ideas please, why the color doesn't change?
Also I know, that I could change the above component to:
private var _color:uint;
<s:Label id="_right" width="25" fontWeight="bold" color="{_color}"/>
and change that _color member, but I'd prefer not do introduce one more data binding, because I'm going to have many ScoreLabel's in my application:
(as you see above, all numbers are green - eventhough setColor(0xFF0000) has been called 3 times).
It seems that without an implicit cast to Label, Flash Builder Premium 4.6 simply won't compile, since it says the following:
Multiple markers at this line: -labelDisplay -1061: Call to a possibly undefined method setStyle through a reference with static type spark.core:IDisplayText.
If you change:
_right.setStyle('color', n);
to this:
(_right as Label).setStyle('color', n);
it should work.