设置spark.components.Label的文本颜色

发布于 2024-12-17 02:33:22 字数 1904 浏览 0 评论 0原文

我有这个非常简单的自定义组件,由两个 标签_left_right

它应该代表游戏中的几种得分: 1.2.3.4.5.6

最后一个数字(上例中的“6”)如果刚刚改变(在当前游戏回合),否则所有数字应该看起来相同。

最后一个数字也应该是红色或绿色(取决于它是“坏”还是“好”分数)。

这是我的 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>

我的问题是:当我调用 myLabel.setColor(0xFF0000); 时,文本不会更改为红色,而是保持默认的绿色 - 即使我可以在调试器中看到痕迹。

请问有什么想法,为什么颜色没有改变?

我还知道,我可以将上述组件更改为:

    private var _color:uint;

    <s:Label id="_right" width="25" fontWeight="bold" color="{_color}"/>

并更改 _color 成员,但我不想再引入一个数据绑定,因为我的中将有许多 ScoreLabel application:

screenshot

(如上所示,所有数字都是绿色 - 尽管 setColor(0xFF0000)有被叫了3次)。

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:

screenshot

(as you see above, all numbers are green - eventhough setColor(0xFF0000) has been called 3 times).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很酷不放纵 2024-12-24 02:33:22

似乎如果没有隐式转换为 Label,Flash Builder Premium 4.6 根本无法编译,因为它说了以下内容:

该行有多个标记:
-标签显示
-1061:通过静态类型spark.core:IDisplayText的引用调用可能未定义的方法setStyle。

如果您将:更改

_right.setStyle('color', n);

为:

(_right as Label).setStyle('color', n);

它应该有效。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文