1 Reply Latest reply on Dec 31, 2016 12:36 AM by anastasiam

    How to show and hide a movie clip with one button.

    larissafc

      I'm pretty much new to Actionscript 3. I know this question has probably been asked and answered before but I just can't seen to get it right.

      What I'm trying to do is when I click this button, this image that I have as a movie clip pops up and if I click the button again it hides, and so on.

      This is the code I have written:

       

      comS.alpha = 0;

      CButton.addEventListener (MouseEvent.CLICK, FunctionName);

      function FunctionName(event:MouseEvent):void

      {

        if (comS.alpha == 0){

        comS.alpha = 1;} else {comS.alpha = 0}

      }

       

      It kinda works, but not really because when I click the button the movieclip shows up and hides again by itself after a second. It works if I erase the first line but I need that because I don't want the movie clip visible before clicking the button first.

      Please help!

        • 1. Re: How to show and hide a movie clip with one button.
          anastasiam Level 1

          Hi,

           

          I tried your code and it seemed to work just fine.

           

          Check your timeline.  Do you have extra frames after the frame where your code is?

           

          If you do, what's happening is that you click the button to make the image appear.  Then, the playhead runs off the 'end' of your timeline, and then starts over again at the start.  There it runs the code again, where you tell it to set comS.alpha back to 0, so the image disappears again.

           

          If you delete the extra frames and make the movie just one frame long, it should behave as you expect.

           

          If you need the movie longer than one frame because you've got other animation or something else going on,  then you'll probably have to think about adding in some stop(); commands or possibly other things to stop the playhead and keep the movie from looping over and over (and re-setting your alpha every time).

           

          Good luck!