Search code examples
actionscript-3flashflash-builderflashdevelop

Remove child from a movie clip with several frames?


I have a movie clip named MC that has two layers. The second layer has 5 frames and the first layer has only one movie clip named item from frame 1 to frame 5.

Then, in my code, I create a new MovieClip named soulItem, and add the item into the soulItem, then add soulItem into MC. Then, the item was removed from his parent (MC).

Then, something wrong happened when the MC goto frame 2. There is a new item instance which its parent is MC, and is not equal to the item in soulItem. There are both displayed on screen, and the numChildren added 1.

the code are as follows:

public function FiveElementBall(MC:MovieClip) {
    var soulItem:MovieClip = new MovieClip();
    var item:MovieClip = MC.getChildByName('item') as MovieClip;
    MC.addChild(soulItem);
}

private function update():void {
   MC.gotoAndStop(2);
}

And I solved this as follows:

private function update():void {
    if(MC.getChildByName('item') != null) {
      dispose(soulItem);
      var soulItem:MovieClip = new MovieClip();
      var item:MovieClip = MC.getChildByName('item') as MovieClip;
      soulItem.addChild(item);//Then, item was removed from MC's Children.
      MC.addChild(soulItem);
    }
    MC.gotoAndStop(2);
}

I dispose and initialize soulItem again when the new item instance occurs (when frame changed).Then it is right.

But I want to know is my guess right and why it is?

And I can solve this remove item from MC when it is a child of MC, Because the soulItem is right, the only fault is that a new instance of item appears.(In one frame, its parent is null, nor soulItem or MC, In other frame, its parent is MC).


Solution

  • But I want to know is my guess right and why it is?

    What's important is where your keyframes are. When the playhead moves to a keyframe that places an instance it will create a new instance if it isn't there already.

    For example, suppose your keyframes in MC look like this:

    timeline

    In this case item has one keyframe on frame 1 and it spans 5 frames. This would not result in the behavior you describe: you could re-parent item and goto frame 2 and a new item instance will not be created.

    However, if you then go back to frame 1 (ex gotoAndStop(1) or prevFrame()), the player hits the keyframe that places the item instance and will create a new instance like you describe, if the instance is not found there.

    Think of a keyframe as a description of what the player expects to exist at that frame. You can change things between keyframes, but when the player reaches a keyframe it will re-evaluate that the display has everything exactly how that keyframe describes, and if not, make it so. There's no way to change a keyframe through code. The best you can do is to avoid moving the playhead to a keyframe that places an instance you intend to modify; for example don't ever go back to frame 1.

    For this reason it's not usually a good idea to move timeline instances with code the way you are doing if you have more than one frame. It's better to do it all by code or all by keyframes.

    For example, instead of having an item instance in the timeline, just export the symbol (for example as Item) and create it by code:

    public function FiveElementBall(MC:MovieClip) {
        var soulItem:MovieClip = new MovieClip();
        var item:Item = new Item();
        soulItem.addChild(item);
        MC.addChild(soulItem);
    }