6 Replies Latest reply on Dec 22, 2017 12:02 AM by JoãoCésar

    One Button to addChild & removeChild

    iezqandarzulqarnaen

      Hi all,

       

      Can anyone show me the proper code to addChild (from lib) & removeChild using only one button.

       

      Seem that my code not working

      button_btn.addEventListener(MouseEvent.CLICK, button_mc_onPress);

      function button_mc_onPress(e: MouseEvent): void

      {

          var popup:popup_class = new popup_class();

          addChild(popup);

          button_btn.removeEventListener(MouseEvent.CLICK, button_mc_onPress);

      }

       

      button_btn.addEventListener(MouseEvent.CLICK, btn_delete_onPress);

      function btn_delete_onPress(e:MouseEvent) : void

      {

          removeChild(popup);

          button_btn.removeEventListener(MouseEvent.CLICK, btn_delete_onPress);

      }

      & got error "1120: Access of undefined property popup."

      I know it's because the "popup" is not yet exist, then i add :

       

      var box_popup:MovieClip;

       

      on top of the codes, the error msg is gone, when i click "button_btn" the "popup" is appear but when I click again to remove the called "popup", its still there.

       

      Thank you.

       

      cc: kglad

        • 1. Re: One Button to addChild & removeChild
          JoãoCésar Adobe Community Professional

          Hi.

           

          Declare your popup variable outside the function definition. Another thing is that you are adding two event handlers to same object that get fired one after the other. So you add your object from Library and remove it immediately after.

           

          One possible solution is this:

          import flash.events.MouseEvent;
          
          var popup:popup_class;
          
          function clickHandler(e:MouseEvent): void
          {
              if (popup)
              {
                  removeChild(popup);
                  popup = null;
              }
              else
              {
                  popup = new popup_class();
                  addChild(popup);
              }
          }
          
          button_btn.addEventListener(MouseEvent.CLICK, clickHandler);
          

           

          Regards,

          JC

          • 2. Re: One Button to addChild & removeChild
            kglad Adobe Community Professional & MVP

            you can do that a little more efficiently:

             

            var popup:popup_class = new popup_class();

            button_btn.addEventListener(MouseEvent.CLICK, button_mc_onPress);

            function button_mc_onPress(e: MouseEvent): void

            {

            if(popup.stage){

            removeChild(popup)

            } else

                addChild(popup);

            }

            }

            • 3. Re: One Button to addChild & removeChild
              iezqandarzulqarnaen Level 1

              Hi kglad & JC,

               

              Thanks for the code, I've apply it to the new AS3 document & worked, but when I apply it into my existed document it's not working.

               

              I have upload the .fla here Dropbox - levelOne_CNC.fla. can you help me look what is wrong with it.

               

              Once you test the .fla click the "CNC Machining Centre" then a info sheet will popup (an addchild) then I want to add another popup (an addChild) beside the existing info sheet by click the red circle with "i" in it. Tried your & JC code but no luck.

               

              Sorry for the trouble.

              • 4. Re: One Button to addChild & removeChild
                JoãoCésar Adobe Community Professional

                Hi again!

                 

                It's nice to know that our code helped you.

                 

                About your FLA, to get it working:

                - Inside of your 'cnc_stone' object, there is a layer with code called 'flash0.ai'. Move it to the top of the stack. Somehow, because of code execution order, I guess, the code in it it's not being called.

                - Inside of your 'infoCarving' object, there is a stop() function in the first frame. Remove it.

                 

                Now it should work.

                 

                Don't hesitate to ask if you have any further questions.

                 

                Regards,

                JC

                • 5. Re: One Button to addChild & removeChild
                  iezqandarzulqarnaen Level 1

                  Dear JC,

                   

                  I've move the infoCarving_btn button to other layer along with the scrip to the top & voilà! it work!

                   

                  Thank you very much.

                   

                  I have another question, will cc you & hope you dont mind.

                   

                  Thanks again JC & kglad both of your code is working.

                  • 6. Re: One Button to addChild & removeChild
                    JoãoCésar Adobe Community Professional

                    Nice!

                     

                    You're welcome!

                     

                    And you can send your questions. No problem. I'll be happy to help.

                     

                     

                    Regards,

                    JC