自定义类中的 ActionScript 3.0 stageWidth

发布于 2024-07-29 07:21:44 字数 659 浏览 2 评论 0原文

如何访问 Costum Class 中的 Stage Class 属性?

类别:

package {
    import Main;
    import flash.events.*;
    import flash.display.Sprite;
    import flash.display.Stage;

    public class Run extends Sprite {
        var obj:a1_spr;


        public function Run() {
            runAssets();

        }



        private function runAssets():void {
            obj = new a1_spr()
            addChild(obj);
            obj.x = stage.stageWidth/2;

        }
    }
}

输出:

TypeError: Error #1009: Cannot access a property or method of a null object reference.

How do I access Stage Class properties in Costum Class?

Class:

package {
    import Main;
    import flash.events.*;
    import flash.display.Sprite;
    import flash.display.Stage;

    public class Run extends Sprite {
        var obj:a1_spr;


        public function Run() {
            runAssets();

        }



        private function runAssets():void {
            obj = new a1_spr()
            addChild(obj);
            obj.x = stage.stageWidth/2;

        }
    }
}

Output:

TypeError: Error #1009: Cannot access a property or method of a null object reference.

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

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

发布评论

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

评论(3

三生一梦 2024-08-05 07:21:44

扩展 Joel 所说的内容,并将其放入上下文中:

每个显示对象都有一个 .stage 属性,但该属性为 null,直到您将显示对象添加到显示列表中。 因此,在构造过程中,您将永远无法访问它,(因为它是在之后添加的)

当您将对象添加到舞台时,事件 ADDED_TO_STAGE 将被触发,让您知道 .stage 属性现在已填充。 之后,您可以从对象中的任何位置访问舞台。

希望能为您澄清一些事情。

To expand on what Joel said, and put it into context:

Every display object has a .stage property, but that property is null until you add you display object onto the display list. So during construction, you will never be able to access it, (because it gets added afterwards)

The event ADDED_TO_STAGE gets fired when you add your object to the stage, ltting you know that the .stage property is now populated. After that happens you can access the stage from anywhere in you object.

Hope that clarifies things for you.

送君千里 2024-08-05 07:21:44
this.addEventListener(Event.ADDED_TO_STAGE, handleAdedToStage)

private function handleAddedToStage(event:Event):void
{
    this.runAssets()
}

private function runAssets():void
{
    obj = new a1_spr();
    addChild(obj);
    obj.x = this.stage.stageWidth/2;
}

您将无法访问构造函数中的阶段(除非您将阶段注入到类中)。 Sprite 具有舞台属性。

this.addEventListener(Event.ADDED_TO_STAGE, handleAdedToStage)

private function handleAddedToStage(event:Event):void
{
    this.runAssets()
}

private function runAssets():void
{
    obj = new a1_spr();
    addChild(obj);
    obj.x = this.stage.stageWidth/2;
}

You aren't going to have access to the stage in the constructor (unless you inject the stage into the class). Sprite has a stage property.

生活了然无味 2024-08-05 07:21:44

当 flash 使用 .as 文件编译 fla 资源时,没有阶段。 因此,代码是作为文档类的准备而启动的,您必须监听是否有一个阶段,以便可以呈现它。

这就是为什么您要监听 ADDED_TO_STAGE ,以检查它是否确实在显示列表中。

所有显示对象都会出现此问题,因为当存在实际舞台时,必须将它们添加到显示列表中。

习惯添加该监听器,并检查阶段。 特别是在团队中工作以及在大型项目中制作自己的组件时。

when flash compiles the fla assets with your .as files, there's no stage. so the code is initiated as preparation for your documentclass, you have to listen to if there's a stage so it can be rendered.

that's why you listen to ADDED_TO_STAGE , to check it's actually in the display list.

This problem occurs for all display objects, since they must be added to the display list when there's an actual stage.

get used to add that listener, and check for a stage. specially when working in a team and your doing your own components in a larger project.

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