Skip navigation
Currently Being Moderated

Remove multiple children and trigger an event if no more child?

Nov 5, 2011 2:27 PM

Tags: #removechild #addchild #removemultiplechild

I'm using for loop to display 3lifes on stage using:

 

 

/////////////////////////////////////////////////////

for(var i=0;i<3;i++) {

          var Life:life = new life();

          Life.x = 100*i+650;

          Life.y = 20;

          addChild(Life);

}

///////////////////////////////////////////////////

 

 

and I have already create a hitTestObject on the character, once the character comes in contact with a movieclip, removeChild(Life);  is triggered. The first child removed successfully but when it comes to the second one, an error appear:

 

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.

          at flash.display::DisplayObjectContainer/removeChild()

          at _4_fla::MainTimeline/hitBoy()[_4_fla.MainTimeline::frame1:52]

          at _4_fla::MainTimeline/dropEnemy()[_4_fla.MainTimeline::frame1:41]

 

What should I do to solve this? and how do I trigger an event after no more life is left?

 
Replies
  • Peter Celuch
    505 posts
    Nov 17, 2005
    Currently Being Moderated
    Nov 5, 2011 2:39 PM   in reply to vincentccw

    Please, paste more code - especially the removeChild() part.

     
    |
    Mark as:
  • Peter Celuch
    505 posts
    Nov 17, 2005
    Currently Being Moderated
    Nov 5, 2011 2:47 PM   in reply to vincentccw

    I would use something like this:

     

    import flash.events.Event;
     
    var lives:Array = [];
     
    for(var i:int = 0; i < 3; i++) {
         addLife();
    }
     
    addEventListener("dropDead", dropDeadHandler);
     
    function addLife():void {
         var Life:life = new life();
         Life.x = 100 * i + 650;
         Life.y = 20;
         addChild(Life);
         lives.push(Life);
    }
     
    // this function triggers when I'm supposed to lose life
    function removeLife():void {
         var Life:life = lives.pop() as life;
         removeChild(Life);
     
         if(lives.length == 0) {
              dispatchEvent(new Event("dropDead"));
         }
    }
     
    function dropDeadHandler(event:Event):void {
         trace("You have no more lives.");
    }
    
     
    |
    Mark as:
  • Peter Celuch
    505 posts
    Nov 17, 2005
    Currently Being Moderated
    Nov 6, 2011 7:57 AM   in reply to vincentccw

    1. Why do you use import flash.events.Event;??

     

    You wanted to dispatch an event.. So I dispatched new Event(); if you use come class, you should import it, otherwise you get a compile error.

     

    2. Your Solution actually works, it removes my second and third lifes without and error, so the tricks is to make individual addchild into an array?

     

    The idea is to store your Life references in an Array, so you can target as many Lives as you want. You used only one variable - Life, which can point only to one object.

     

     

    3. I try running :

        removeLife();

      removeLife();

      removeLife();

    for third times but You have no more lives. doesn't seem to triggered in the output window?

     

    Did you addEventListener("dropDead"...) ? Use trace method to print out lives.length:

     

    4. when I'm running removeLife() for fourth time, an error occurs?

     

    TypeError: Error #2007: Parameter child must be non-null.

     

    Sorry, didn't make it bulletproof. You should stop the game when you have 0 lives anyway, so this shouldn't happen. But try this instead:

     

    // this function triggers when I'm supposed to lose life
    function removeLife():void {
        if(lives.length == 0) {
            trace("What are you doing here? You should be dead already!");
            return;
        }
     
        var Life:life = lives.pop() as life;
        removeChild(Life);
     
        trace("You have still " + lives.length + " lives.");
        if(lives.length == 0) {
            trace("Baam! You're dead. Dispatching event.. 'hope somebody is listening.");
            dispatchEvent(new Event("dropDead"));
        }
    }
    
     
    |
    Mark as:
  • Peter Celuch
    505 posts
    Nov 17, 2005
    Currently Being Moderated
    Nov 6, 2011 12:43 PM   in reply to vincentccw

    Yes, you're right. Since Array::pop() returns type *, you can assign it to variable of any type, I'm just used to casting... but both statements are valid.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points