Skip to main content
Participating Frequently
May 17, 2010
Question

How to disable a button once it has been clicked

  • May 17, 2010
  • 2 replies
  • 751 views

Hi!

I need to disable a button once the user has clicked it! I also need the colour of the button to change once it has been clicked as well (I need it to just grey out - not sure if this can be solved using actionscript??)

The only method I could think of of accomplishing this is to put each button in their own UILoader, but I'm hoping there is a simpler method!?

This topic has been closed for replies.

2 replies

dikuno10
Known Participant
May 20, 2010

Here's what you do:

1. Open the Components panel and drag a button on the Stage (do NOT create button symbol).

2. For now, give it an instance name of "aButton" - that's what the code below uses. However, you probably want to use a different name. Also, you can open up the Components Inspector panel (CS4) to change the label on the button - use the Component Parameters section on the Properties panel if you have CS5.

3. In the Actions panel use this code:

aButton.addEventListener(MouseEvent.CLICK, disableButtonHandler);
function disableButtonHandler(event:MouseEvent):void
{

     //do whatever (gotoAndStop, fscommand...)

     aButton.enabled = false;
}

4. Test it. If it was done correctly (I tested it on my computer), the button should deactivate and go greyish when clicked.

Make sure you use a Button Component. I don't know it this code would work with a Button or a MovieClip symbol.

May 17, 2010

The simplest way is to just remove the listener and change the alpha. Something like so:

myButton.buttonMode = true;

myButton.addEventListener(MouseEvent.CLICK, btnClicked, false, 0, true);

function btnClicked(e:MouseEvent)

{

     //do whatever

     myButton.removeEventListener(MouseEvent.CLICK, btnClicked);

     myButton.buttonMode = false;

     myButton.alpha = .3;

}