使用ActionScript更改标签文本

时间:2011-12-01 16:56:53

标签: actionscript-3 flex actionscript

我有一个非常基本的问题。为什么这不起作用?!

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               width="1000" height="550" minWidth="960" backgroundColor="#F2F0F0">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <s:Label id="test1" x="43" y="259" text="Label"/>

    <fx:Script>
        <![CDATA[
            test1.text = "Yay! This works...!";
        ]]>
    </fx:Script>
</s:Application>

我收到此错误:访问未定义的属性。

谢谢!

1 个答案:

答案 0 :(得分:5)

您在创建组件之前设置了文本。尝试将赋值放入方法并在creationComplete上调用方法:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
           xmlns:s="library://ns.adobe.com/flex/spark"
           xmlns:mx="library://ns.adobe.com/flex/mx"
           width="1000" height="550" minWidth="960" backgroundColor="#F2F0F0" 
           creationComplete="onCreationComplete()">
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Label id="test1" x="43" y="259" text="Label"/>

<fx:Script>
    <![CDATA[
        public function onCreationComplete():void {
            test1.text = "Yay! This works...!";
        }
    ]]>
</fx:Script>
</s:Application>