addChild自定义类AS3

发布于 2024-12-06 08:24:55 字数 1132 浏览 0 评论 0原文

这是我尝试实例化到主类中的类:

public class Character extends Sprite {

    [Embed(source='../lib/front1.svg')]
    private var front1Class:Class;
    private var crosshair:Sprite = new front1Class ();

    public function Character() {
        trace("started");
        Mouse.hide();

        crosshair.scaleX = 5;
        crosshair.scaleY = 5;
        this.addChild(crosshair);

        stage.addEventListener(Event.ENTER_FRAME, MrEveryFrame);
        stage.addEventListener(MouseEvent.CLICK, click);
    }


    private function click(evt:MouseEvent):void {
        trace("clicked @ " + evt.stageX + "," + evt.stageY);

    }
    public function MrEveryFrame(e:Event):void
    {
        crosshair.x = mouseX - 15;
        crosshair.y = mouseY - 15;


    }

}

当我将其设置为文档类时,它工作正常。

但是...当我将其设为我的文档类并尝试从那里调用它时:

public class Shell extends Sprite
{


    private var character:Sprite = new Character ();


    public function Shell() 
    {

        addChild(character);


    }


}

它会中断,并且不再显示精灵对象(尽管它确实擦除了鼠标指针)。

这是怎么回事?您无法将自定义精灵或影片剪辑类实例化为 DisplayObject 类???

This is the class I am trying instantiate into my main class:

public class Character extends Sprite {

    [Embed(source='../lib/front1.svg')]
    private var front1Class:Class;
    private var crosshair:Sprite = new front1Class ();

    public function Character() {
        trace("started");
        Mouse.hide();

        crosshair.scaleX = 5;
        crosshair.scaleY = 5;
        this.addChild(crosshair);

        stage.addEventListener(Event.ENTER_FRAME, MrEveryFrame);
        stage.addEventListener(MouseEvent.CLICK, click);
    }


    private function click(evt:MouseEvent):void {
        trace("clicked @ " + evt.stageX + "," + evt.stageY);

    }
    public function MrEveryFrame(e:Event):void
    {
        crosshair.x = mouseX - 15;
        crosshair.y = mouseY - 15;


    }

}

When I set it to the document class, it works fine.

However... when I make THIS my document class and try to call it from there:

public class Shell extends Sprite
{


    private var character:Sprite = new Character ();


    public function Shell() 
    {

        addChild(character);


    }


}

It breaks, and no longer shows the sprite object (though it does erase the mouse pointer).

What's the deal here? You can't instantiate custom sprite or movieclip classes into a DisplayObject class???

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

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

发布评论

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

评论(1

眼眸 2024-12-13 08:24:55

构造函数中的阶段为 null。正如您自己发现的那样,只有当您的类是文档类时,这才有效。因此,请像这样更改您的构造函数:

public function Character() {
    trace("started");
    Mouse.hide();

    crosshair.scaleX = 5;
    crosshair.scaleY = 5;
    this.addChild(crosshair);

    addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}

private function onAddedToStage(event:Event):void
{

    stage.addEventListener(Event.ENTER_FRAME, MrEveryFrame);
    stage.addEventListener(MouseEvent.CLICK, click);
}

添加侦听器将仅在已知阶段后才访问该阶段,并且它不再是 null

The stage is null in the constructor. That only works when your class is the Document Class, as you found out yourself. So change your constructor like this:

public function Character() {
    trace("started");
    Mouse.hide();

    crosshair.scaleX = 5;
    crosshair.scaleY = 5;
    this.addChild(crosshair);

    addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}

private function onAddedToStage(event:Event):void
{

    stage.addEventListener(Event.ENTER_FRAME, MrEveryFrame);
    stage.addEventListener(MouseEvent.CLICK, click);
}

Adding the listener will access the stage only after the stage is known, and it's no longer null

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