2009-11-16 91 views
0

嘿peepz!我有這個頁腳圖像,我想對齊到舞臺的底部,但是我收到了錯誤。嘗試使用stage.height放置圖形時出現錯誤

正如你所看到的,我在構造函數中有一個ADDED_TO_STAGE監聽器。

package src.display{ 

import flash.text.*; 
import flash.display.*; 
import flash.geom.Matrix; 
import flash.events.Event; 

public class Frame extends Sprite { 
    private var footer:Sprite = new Sprite(); 

    // ☼ ------ Constructor 
    public function Frame():void { 
     this.addEventListener(Event.ADDED_TO_STAGE, tracer); 
    } 

    public function tracer(event:Event) { 
     trace("Frame added to stage --- √"+"\r"); 
     this.removeEventListener(Event.ADDED_TO_STAGE, tracer); 
    } 

    // ☼ ------ Init 
    public function init():void { 
     footer.graphics.beginFill(0x000); 
     footer.graphics.drawRect(0,0,800,56); 
     footer.graphics.endFill(); 
     footer.y = (stage.height - footer.height); // <-- This Line 

     addChild(footer); 
    } 

} 

}

電影將正常工作,如果我註釋掉線26(當然我不希望Y中0):

footer.y = (stage.height - footer.height); 

以下是錯誤在輸出窗口中出現:

TypeError:錯誤#1009:無法訪問空對象引用的屬性或方法。 在src.display ::框架/的init()[/用戶/ lgaban /項目/播放器/ SRC /顯示/ Frame.as:26]


UPDATE

回答我自己quesiton,fix here

回答

1

使用自定義事件有點矯枉過正,特別是當您已經將監聽器添加到舞臺時。我會做這樣的:

package src.display{ 

    import flash.text.*; 
    import flash.display.*; 
    import flash.geom.Matrix; 
    import flash.events.Event; 

    public class Frame extends Sprite { 

     // don't instantiate your sprite here, it's weird! :) 
     private var footer:Sprite; 

     // this is the same as in your example 
     public function Frame():void { 
      this.addEventListener(Event.ADDED_TO_STAGE, handleAddedToStage); 
     } 

      // i renamed this to reflect what it does 
     private function handleAddedToStage(event:Event) { 
      trace("Frame added to stage --- √"+"\r"); 
      this.removeEventListener(Event.ADDED_TO_STAGE, handleAddedToStage); 
      init(); 
     } 

     // this is also essentially the same, except for private since it shouldn't be called from the outside 
     private function init():void { 
      footer = new Sprite(); 
      footer.graphics.beginFill(0x000); 
      footer.graphics.drawRect(0,0,800,56); 
      footer.graphics.endFill(); 
      footer.y = (stage.height - footer.height); 

      addChild(footer); 
     } 

    } 
} 
+0

啊是啊,這是高清做到這一點。在我主我也調用Frame.init功能,因爲我需要傳遞一個變種更清潔的方式,但我像這樣很多:) – 2009-11-17 16:18:28

1

不是說它是完整的答案,而是錯誤告訴你該階段爲空。