ActionScript 将Child 从类添加到舞台

发布于 2024-11-12 05:06:23 字数 370 浏览 3 评论 0原文

我正在尝试通过 addChild() 方法将对象添加到我的主舞台。通常,在 FLA 本身内工作时,使用 addChild(myObject) 效果很好。但是,它在外部 AS 类中不起作用。

我发现的所有其他教程都向我展示了如何做这样的事情,但这不是我需要的:

var myMovieClip:MovieClip = new MovieClip();
myMovieClip.addChild(myObject); // Works great, but not what I need

有人可以告诉我如何通过外部 AS 类将对象添加到我的主阶段

感谢您抽出时间。

I am trying to add an object to my main stage via the addChild() method. Normally, when working within the FLA itself, using addChild(myObject) works just fine. However, it does not work within an external AS class.

All of the other tutorials I have found showed me how to do something like this, which is not what I need:

var myMovieClip:MovieClip = new MovieClip();
myMovieClip.addChild(myObject); // Works great, but not what I need

Could someone please show me how to add an object to my main stage via an external AS class.

Thank you for your time.

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

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

发布评论

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

评论(3

灼疼热情 2024-11-19 05:06:23

如果这是您的文档类或具有有效阶段引用的类,您可以使用:

stage.addChild(myObject);

编辑 - 添加示例。

下面是一个扩展 EventDispatcher 并传递“stage”参数的示例类。

首先,类:

package {

    import flash.display.MovieClip;
    import flash.display.Stage;
    import flash.events.EventDispatcher;

    public class MyClass extends EventDispatcher {

        private var _stage:Stage;

        public function MyClass(stage:Stage) {
            _stage = stage;

            var mc:MovieClip = new MovieClip();
            _stage.addChild(mc);
        }
    }
}

以及用法(假设这是来自引用“stage”的类):

var obj:MyClass = new MyClass(null, this.stage);

If this is your document class or a class that has a valid stage reference, you could use:

stage.addChild(myObject);

EDIT - Added example.

Here is an example class that extends EventDispatcher and passes a parameter of the 'stage'.

First, the class:

package {

    import flash.display.MovieClip;
    import flash.display.Stage;
    import flash.events.EventDispatcher;

    public class MyClass extends EventDispatcher {

        private var _stage:Stage;

        public function MyClass(stage:Stage) {
            _stage = stage;

            var mc:MovieClip = new MovieClip();
            _stage.addChild(mc);
        }
    }
}

And the usage (assuming this is from a class that has reference to 'stage'):

var obj:MyClass = new MyClass(null, this.stage);
初见终念 2024-11-19 05:06:23

您是否已经尝试过以下操作?

stage.addChild(myObject)

Did you already try the following?

stage.addChild(myObject)
沙沙粒小 2024-11-19 05:06:23

科里的方法有效。您不需要像他在示例中那样扩展 EventDispatcher。

他的示例中重要的是:

private var _stage:Stage;
public function MyClass(stage:Stage) {
_stage = stage;

var mc:MovieClip = new MovieClip();
 _stage.addChild(mc);
}

注意他如何使用 _stage 变量来引用它。

Corey's method works. You don't need to extend EventDispatcher like he did in his example.

What is important in his example is:

private var _stage:Stage;
public function MyClass(stage:Stage) {
_stage = stage;

var mc:MovieClip = new MovieClip();
 _stage.addChild(mc);
}

Notice how he uses the _stage variable referencing it.

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