0

嘿,窥视!我有这个页脚图像,我想与舞台底部对齐,但是我遇到了错误。

如您所见,我在构造函数中有一个 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::Frame/init()[/Users/lgaban/Projects/Player/src/display/Frame.as:26]


更新

回答了我自己的问题,在这里修复

4

2 回答 2

1

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

于 2009-11-16T22:35:07.493 回答
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);
        }

    }
}
于 2009-11-17T16:07:37.037 回答