2012-07-14 212 views
0

我知道網上有一百萬個關於AS3編譯器錯誤1120: Access of undefined property <property>的問題,但這種情況很奇怪。Flex編譯器錯誤1120

我正在剝皮Flex 4.6中的<s:Application>組件,並且我在皮膚MXML文件中。該線super.addEventListener(Event.ADDED_TO_STAGE, positionObjects);給我的問題說:1120: Access of undefined property positionObjects。但positionObjects在它下面聲明。任何想法有什麼不對?

<fx:Script> 
    <![CDATA[ 
     /** 
     * @private 
     */ 
     override protected function updateDisplayList(unscaledWidth:Number, 
      unscaledHeight:Number) : void 
     { 
      bgRectFill.color = getStyle('backgroundColor'); 
      bgRectFill.alpha = getStyle('backgroundAlpha'); 
      super.updateDisplayList(unscaledWidth, unscaledHeight); 
     } 

    //Listen for when objects are added to the stage, before positioning them 
     [Bindable] 
     private var logoX:Number = 0; 

     super.addEventListener(Event.ADDED_TO_STAGE, positionObjects); 

     private function positionObjects(e:Event):void { 
      this.logoX = stage.stageWidth/3; 
     } 
    ]]> 
</fx:Script> 

謝謝你的時間。

回答

0

我想通了爲什麼我得到一個錯誤。我沒有考慮並假設我可以在初始化處理程序之外添加一個事件監聽器。出於某種原因,我認爲AS3將通過編譯的<fx:Script>標籤執行解釋程序執行程序腳本的方式。

由於某種原因,我再也沒有想過爲<s:Skin>標籤添加addedToStage屬性,並讓它從那裏執行positionObjects方法。一切都很好。

2

你不能像fx:Script塊中有可執行的實現:

<fx:Script> 
    super.addEventListener(Event.ADDED_TO_STAGE, positionObjects); 
</fx:Script> 

這應該從生命週期的函數被調用,例如創建完整:

<s:Skin xmlns:fx="http://ns.adobe.com/mxml/2009" 
     xmlns:s="library://ns.adobe.com/flex/spark" 
     xmlns:fb="http://ns.adobe.com/flashbuilder/2009" 
     alpha.disabled="0.5" 
     alpha.disabledWithControlBar="0.5" 
     creationComplete="skin1_creationCompleteHandler(event)"> 

    <fx:Script fb:purpose="styling"> 
     <![CDATA[ 

      /* your implementation, same as before... */ 

      protected function skin1_creationCompleteHandler(event:FlexEvent):void 
      { 
       // move your event listener to this function. 
       super.addEventListener(Event.ADDED_TO_STAGE, positionObjects); 
      } 
     ]]> 
    </fx:Script> 
</s:Skin> 
+0

是的,謝謝。我剛纔幾秒鐘就回答了我自己的問題。 +1 – 2012-07-15 00:25:02