Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
Dec 30, 2016 Dec 30, 2016

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!

6.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Participant , Dec 31, 2016 Dec 31, 2016

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 sh

...
Translate
Participant ,
Dec 31, 2016 Dec 31, 2016
LATEST

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines