7 Replies Latest reply: Aug 16, 2012 8:50 AM by Taikey RSS

    Removing Movie Clip using RemoveChild Help

    Taikey

      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);

      }

        • 1. Re: Removing Movie Clip using RemoveChild Help
          Ned Murphy MVP

          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

          • 2. Re: Removing Movie Clip using RemoveChild Help
            Ned Murphy MVP

            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);
            }

            • 3. Re: Removing Movie Clip using RemoveChild Help
              Taikey Community Member

              Thanks for the fast reply !
              Yeah , i kinda got that i am adding the MC1 and then deleting it after .

               

              Your code has helped. Thanks for taking the time to solve it for me

              I'm still kinda wondering tho, My lecturer had used the code with an if statement and contains()
              do you know how to apply the both of them? 

               

               

              I

              • 4. Re: Removing Movie Clip using RemoveChild Help
                Ned Murphy MVP

                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))

                • 5. Re: Removing Movie Clip using RemoveChild Help
                  Taikey Community Member

                  Yea, i was using contains to check .
                  Well originally im going to be making some thumbnails, each thumbnail storing a movieclip . Kinda like a image gallery except im using movie clips .

                   

                   

                  I was thinking of using contains to check if (myMC1) is still at stage after clicking on thumbnail2,3, or etc , if it is still on stage, removeChild(myMC1) , so that if any other thumbnails were to be clicked on, thumbnail1's movieclip on stage will be removed. That's where im stuck and confused on. Can i ask for your guidance? Thanks and sorry once again for taking up your time .

                  • 6. Re: Removing Movie Clip using RemoveChild Help
                    Ned Murphy MVP

                    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.

                    • 7. Re: Removing Movie Clip using RemoveChild Help
                      Taikey Community Member

                      Why Yes, your right . I think i kind of understand this now. Will reread thru more.

                      Thanks for the help you provided. and may God bless you