2009-11-16 93 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

不是说它是完整的答案,而是错误告诉你该阶段为空。