为什么选择 Event.ADDED_TO_STAGE

发布于 2024-10-15 16:16:35 字数 182 浏览 9 评论 0原文

如果我的构造函数中没有以下内容,我的代码经常会出现问题:

addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);

一旦我在将 MovieClip 或文档类添加到舞台后执行代码,它似乎工作得更好...但这是为什么呢?

I often have problems with my code if I don't have the following in my constructor:

addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);

Once I execute code after my MovieClip or Document Class has been added to stage, it seems to work better...but why is this?

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

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

发布评论

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

评论(2

╰◇生如夏花灿烂 2024-10-22 16:16:35

这样想:如果您的类正在被构造,它可能来自一些看起来像这样的托管代码:

var newComponent = new TheComponent();
parentElement.addChild(newComponent)

因此,如果您在构造函数中执行代码,那么您正在执行尚未完全连接的代码。例如,您的构造函数中永远不会有父级,因为您尚未添加到层次结构中。

当然,有些事情会起作用……例如,任何不依赖于可视树一部分的代码都可以起作用。但是,任何依赖于知道属于更大系统的一部分的代码都需要在添加到阶段后执行。

这有帮助吗?

Think of it this way: if your class is being constructed, it MIGHT be from some hosting code that looks something like this:

var newComponent = new TheComponent();
parentElement.addChild(newComponent)

So, if you are executing code in the constructor, you are executing code that is not fully wired up yet. For instance, you will never have a parent in your constructor because you haven't been added to the hierarchy yet.

Of course, some things will work... any code that does not rely on being part of the visual tree, for instance, will work. BUT, any code that relies on knowing that is part of a bigger system will need to execute AFTER it is added to the stage.

Does that help?

橙味迷妹 2024-10-22 16:16:35

添加到 Brian 的解释中,ADDED_TO_STAGE 的作用是使单个类中的对象管理更加容易。通过让对象知道何时在该阶段添加和删除它,您可以让它完全管理自己。例如,在处理程序方法中启动动画或在删除时停止动画。通常,如果没有该事件,您将必须让对象将其添加到舞台开始和停止。

将事件框架视为一个自动化框架,每当广播特定事件时都会调用方法/函数。因为 ADDED_TO_STAGE 是在 addChild 方法中的对象上分派的,所以它类似于调用自定义方法 bind()。

package{
 class Main extends Sprite{
  public Main(){
   // called on instantiation.
  }
  public function bind():void{
   //called later in the stack, either by ADDED_TO_STAGE or main.bind()
  }
 }
}

这是因为这些属性( x,y,alpha )正在 addChild 方法中初始化。这样做是出于内存管理的原因。为什么要为那些在对象“到达”舞台之前不会被处理的东西分配一些字节呢?

Adding onto Brian's explanation, ADDED_TO_STAGE is there to make management of an object within a single class easier. By allowing the object to know when it's added and removed from that stage, you have have it manage it's self entirely. Say, starting an animation in the handler method or stopping it when removed. Normally without that event you would have to have the object adding it to the stage start and stop.

Think of the event framework as an automation framework that called methods/functions whenever a particular event is broadcasted. Because the ADDED_TO_STAGE is dispatched on and object in the addChild method, it's similar as to calling a custom method bind().

package{
 class Main extends Sprite{
  public Main(){
   // called on instantiation.
  }
  public function bind():void{
   //called later in the stack, either by ADDED_TO_STAGE or main.bind()
  }
 }
}

It's because those properties ( x,y,alpha ) are being initialized in the addChild method. This is done for memory management reasons. Why allocate a few bites for something that will not be processed until the object 'hits' the stage.

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