Weird parent thing (bug?) in Flash

I thought that when a MovieClip has a stage the MovieClip is on the displaylist and when a MovieClip has a parent, you always could do “this.parent.removeChild(this)”. But that is not true.

Check out this example.

The clip on the timeline is only on frame 1. But when the timeline is on frame 2 the clip is still tracing a parent and a stage. Weird enough the parents numChildren is 0. Even “this.parent.contains(this)” results true. So how could you ever check if a MovieClip is on the displaylist?

After 10 frames I try to the remove the Clip from his parent, but that results in an error. How can you prevent that, without using try-catch?

2 Responses to “Weird parent thing (bug?) in Flash”

  1. Patrick Says:

    Hi Thijs!

    Animations on timeline according to code is always a pain in the ass! My guess is that you’ve assigned the enterframe event on the actual mc that is put on stage in timeline and not by code. The frame of this clip is async with the main timeline frame, so the mc isn’t initialized (the added_to_stage event is never fired, only the removed_from_stage will be fired). It’s odd that the timeline contains the actual clip while the timeline hasn’t any children!

  2. Thijs Says:

    Hey Patrick,

    Timeline vs code is always tricky. The best practice is to not combine them. But sometimes you don’t have a choice.

    But actually the added_to_stage event is also fired on timeline clips. But you can’t catch them with timeline code, since the event is already dispatched before you could add the listener.

    So I kind of solved to problem to create a class for all objects and set listeners to the ‘added’ and ‘removed’ events to keep track of the object has a parent or not. If the object has a parent (and .parent is not null) you can safely do ‘this.parent.contains(this)’. Otherswise I’ll do ‘this.parent.contains(this)’ within a try-catch block. I know, it’s still dirty…

Leave a Reply