2011-12-01 99 views
0

我得到了非常基本的問題。爲什麼這不起作用?使用動作腳本更改標籤文本

<?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> 

我得到了這個錯誤:訪問未定義的屬性。

謝謝!

回答

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> 
+0

+1謝謝,它的工作原理。 – enloz