尝试使用 stage.height 放置图形时出现错误

发布于 2024-08-11 04:54:08 字数 1382 浏览 6 评论 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: Error #1009:无法访问空对象引用的属性或方法。 在 src.display::Frame/init()[/Users/lgaban/Projects/Player/src/display/Frame.as:26]


更新

回答了我自己的问题,在此处修复

Hey peepz! I have this footer image, that I want to align to the bottom of the stage, however I'm getting errors.

As you can see I have an ADDED_TO_STAGE listener in the constructor function.

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);
    }

}

}

The movie will work correctly if I comment out line 26 (but of course I don't want Y to be 0):

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

Here is the error in the output window I'm getting:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at src.display::Frame/init()[/Users/lgaban/Projects/Player/src/display/Frame.as:26]


UPDATE

Answered my own quesiton, fix here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

各自安好 2024-08-18 04:54:08

使用自定义事件有点矫枉过正,特别是当您已经将监听器添加到舞台时。我会这样做:

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);
        }

    }
}

Using a custom event is a bit overkill, especially when you have the listener for added to stage already in there. I would do it like this:

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);
        }

    }
}
喜你已久 2024-08-18 04:54:08

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

Not that it is the complete answer, but that error is telling you that stage is null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文