0

I'm aware that addChild (1) adds the object into a display container object, and (2) shows the object in the DisplayObjectContainer. What I want to know is what happens when an object is added into a DisplayObjectContainer?

To summarize my question is (are),

  1. Is there any magic happens when addChild invoked? i.e. Something is happening in, for example, DisplayObjectContainer.
  2. Let's say, I have MovieClip A and B. B has A as part of it. I called addChild (A) in B. In MainTimeline and Stage, I don't call addChild (B). Visually, since B hasn't been added, A won't be there too. However, does A is exist (e.g. the memory allocated), even though it's not on the Stage?

I have searched here and there in SO, but humongous amount of the questions asked related to mine are technical. I greatly appreciate any answer or pointer regarding my questions

mghazian
  • 30
  • 6

1 Answers1

2

Here is simplified explanation of what happens.

When you create MovieClip A (or any object) using the new keyword (or if using FlashPro an item that exists on the timeline where the playhead is), that object is in memory. So whether or not MovieClip A is on the display list, it is taking up memory in your application.

When you use addChild, here are some of those things that happen (not necessarily in order):

  1. If the object being added already has parentage, it is removed from that parent (though scale and position are kept, and will now be relative to the new parent).

  2. Events are dispatched on the new parent (and the old parent if applicable). Event.ADDED and Event.ADDED_TO_STAGE + Event.REMOVED / REMOVED_FROM_STAGE on the old parent.

  3. The parent(s) of the newly added object, will now take into account the new child/grandchild. Things like hit tests, and bounds, and mouse overs etc.

  4. On the next frame tick (the stage's refresh rate), that item will be drawn

All that said, once a movieClip is on the display list, there is performance drain VS just having that object in memory - so if an object doesn't need to be seen, it's more efficient to have it off the display list until it needs to be seen.

If using FlashPro/Animate, you should also be aware that if through code you do anything that manipulates the parentage of a timeline display object (addChild/removeChild/setChildIndex etc), that timeline object will no longer be managed by the timeline. That means that if you have an empty keyframe to remove an item from the timeline, that item will actually stay on the screen until you either explicitly remove it (removeChild(item)) or it's parent goes away.

BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40
  • Thank you for the answer. I want to clarify this: '(I)t's more efficient to have it off the display list until it needs to be seen.'. Referring to [another answer I stumbled upon when I searched for similar question](http://stackoverflow.com/questions/20123119/flash-game-simple-performance-tricks/20123969#20123969), wouldn't it be better if I load them all first, and place it somewhere off-screen, only to be placed on-screen when they're needed? – mghazian Jun 15 '16 at 04:38
  • It's very dependent on the nature of your application. From a garbage collection / memory standpoint, it's certainly more performant to create all your objects prior to the part of your application that needs maximum performance (you want to avoid using the `new` keyword at any moment where performance is critical). If you're in a situation where you would be using `remove/addChild` very frequently on the same item, then yes, it would be better to just move it's position out of the stage bounds. – BadFeelingAboutThis Jun 15 '16 at 18:40