7 Replies Latest reply: May 9, 2012 12:40 PM by Ned Murphy RSS

    closing a movie clip with EventListener with a mouse click

    goodsnow82

      I have a button that loads a movie clip and I want to be able to close that same movieclip by clicking anywhere.

       

      This is my code so far:

       

      btn_2.addEventListener(MouseEvent.CLICK, goBio);

      function goBio (e:MouseEvent):void {

      var newBio:BioToc = new BioToc();

      this.addChild(newBio);

      newBio.x = 25;

      newBio.y = 45;

      }

        • 1. Re: closing a movie clip with EventListener with a mouse click
          Ned Murphy MVP

          Since that object is declared inside a function it will be harder to target, so move the declaration outside the function...

           

          var newBio:BioToc;

           

          and just use

           

          newBio =  new BioToc();

           

          inside the function.

           

          Then you will be able to directly target the newBio instance.

           

          To remove it, you can add a MOUSE_UP event listener for the stage inside the function and in the event handler function for that new listener just use this.removeChild(newBio);

          • 2. Re: closing a movie clip with EventListener with a mouse click
            goodsnow82 Community Member

            I got the first part but I have no idea what the AS should look like for that second part to close the mc?

            • 3. Re: closing a movie clip with EventListener with a mouse click
              Ned Murphy MVP

              It would be much like your code for the button that you already have, except the stage would be assigned the listener instead of the button and the event would be a MOUSE_UP instead of a CLICK.  The function that you create for it would just need the one line I showed earlier.

              • 4. Re: closing a movie clip with EventListener with a mouse click
                goodsnow82 Community Member

                So how would you assign the listener to the stage?

                 

                This is my code now:

                 

                btn_2.addEventListener(MouseEvent.CLICK, goBio);

                function goBio (e:MouseEvent):void {

                var newBio = new BioToc();

                this.addChild(newBio);

                newBio.x = 25;

                newBio.y = 45;

                }

                 

                stage.addEventListener(MouseEvent.MOUSE_UP, goBio);

                function goBio (e:MouseEvent):void {

                var newBio = new BioToc();

                this.removeChild(newBio);

                newBio.x = 25;

                newBio.y = 45;

                }

                • 5. Re: closing a movie clip with EventListener with a mouse click
                  Ned Murphy MVP

                  You got very close except for a few things...

                   

                  You now have two functions named goBio, which is not going to sit well with the compiler.  Assign a different name for the new function. 

                   

                  Also, what was the only line I said you needed in the function for removing the movieclip?

                   

                  Lastly, you still haven't done the part you said you understood from my first response.  If you don't do it, your second function will not be able to target the movieclip

                  • 6. Re: closing a movie clip with EventListener with a mouse click
                    goodsnow82 Community Member

                    If I remove var from this line var newBio = new BioToc(); then it stops working all together. So I'm not sure what you mean by just changing it to newBio = new BioToc();

                     

                    I'm not sure what you mean by changing goBio, can I just put any word in front of Bio to change it?

                     

                    This is what I did and now I get an error that says "Scene 1, Layer 'actions', Frame 1, Line 10. 1084: Syntax error: expecting leftparen before this.

                     

                    btn_2.addEventListener(MouseEvent.CLICK, goBio);

                    function goBio (e:MouseEvent):void {

                    var newBio = new BioToc();

                    this.addChild(newBio);

                    newBio.x = 25;

                    newBio.y = 45;

                    }

                     

                    stage.addEventListener(MouseEvent.MOUSE_UP, removeBio);

                    function removeBio this.removeChild(newBio);

                    var newBio = new BioToc();

                    newBio.x = 25;

                    newBio.y = 45;

                    }

                    • 7. Re: closing a movie clip with EventListener with a mouse click
                      Ned Murphy MVP

                      There is nothing for me to gain in writing your code for you, but there is for you.  If you go back thru everything I have offered and fix your code to what I describe you will not have no errors.  As it goes now, you have several and you have not done anything I have indicated you  should do except for assigning the stage an event listener correctly.