Skip navigation
Taikey
Currently Being Moderated

Removing Movie Clip using RemoveChild Help

Aug 16, 2012 4:39 AM

Hi, im new to this forum, and new to actionscript 3.


I'm wondering, how can you remove the movieclip so that it doesnt stack when you click another button.


sh1.png


Btn 1 opens the movieclip by addchild


s2.png


Btn 2 opens the 2nd movieclip but stacks in front , and the first movieclip isnt removed


s3.png

AS linkage of my movieclip so that i can bring it to stage.


My code



thumbnail1.addEventListener(MouseEvent.CLICK ,myThumb1);


function myThumb1(evt:MouseEvent):void

{

var myMC:hover1 = new hover1();

container.addChild(myMC);


if(contains(myMC))

{

container.removeChild(myMC)

}

}


thumbnail2.addEventListener(MouseEvent.CLICK ,myThumb2);

function myThumb2(evt:MouseEvent):void

{

var myMC2:hover2 = new hover2();

container.addChild(myMC2);

}

 
Replies
  • Currently Being Moderated
    Aug 16, 2012 5:12 AM   in reply to Taikey

    By declaring the vars inside the function, you remove any ability to target them directly by name outside of the function.  So one way to deal with this would be to declare myMC1 and myMC2 outside of any functions so that every function can target them.

     

    An alternative approach would be to just empty the container out before adding any children to it using the removeChildAt() method (you need to check that there are children within it to remove beforehand)

     

    For the code you show, your myThumb1 function isn't going to do much for you since it is adding myMC1 and removing it immediately after that

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 16, 2012 5:20 AM   in reply to Taikey

    Here is your code rewriten using the second approach I described...

     

    thumbnail1.addEventListener(MouseEvent.CLICK ,myThumb1);

     

    function myThumb1(evt:MouseEvent):void
    {

         while(container.numChildren > 0){
             container.removeChildAt(0);
         }

     

         var myMC:hover1 = new hover1();
         container.addChild(myMC);
    }

     

    thumbnail2.addEventListener(MouseEvent.CLICK ,myThumb2);

     

    function myThumb2(evt:MouseEvent):void
    {
         while(container.numChildren > 0){
              container.removeChildAt(0);
         }


         var myMC2:hover2 = new hover2();
         container.addChild(myMC2);
    }

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 16, 2012 6:26 AM   in reply to Taikey

    For the code you showed, I would guess you wanted to check if it contains the other other mc (myMC2), not the one you just added because the one you just added is definitely there.  Also, you probably want to specify it for the container since that is where you place the instances, as in...

     

    if(container.contains(myMC2))

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 16, 2012 7:23 AM   in reply to Taikey

    I think I already provided the guidance you need, you just need to reread what I answered already and think it thru... if you are adding myMC1, which mc do you need to remove (not myMC1, right?)?

     

    If you are having trouble with your assignment I suggest you either work with one of your classmates or your instructor to help resolve your understanding.

     
    |
    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