Create a new Actionscript file. Type in the following, which is just the structure of a basic class (the stuff you always have to type in when you write a class!). Note the line that contains the package statement. We're going to save this to a package. If you want, substitute the word "mysite" for the actual name of your website, or whatever name you'd like to use as a place to store your classes. If you're confused about any of this, check out my first tutorial, Make your own reusable classes using Flash and AS3 and at least read the first two pages, where I explain packages and the classpath.
package com.mysite.utils {
   
    public class ButtonDisabler {
       
        private var buttons:Array = new Array();
       
        public function ButtonDisabler (theButtons:Array) {
            buttons = theButtons;
        }
    }
}

Save the file as "ButtonDisabler.as" in the com.mysite.utils folder. If the utils folder doesn't exist, create it. If none of the folders exist, you didn't read my first tutorial! Click the link above, learn about packages and the classpath, and then come back.

All the class does so far is accepts an array and stores it as a private class member (or property). When our client code (the code in the fla file) creates a new instance of this class, it must pass an array as an argument in the parentheses. Specifying an array as a parameter here, and not giving it a default value, makes it a required parameter. But that's okay, as this class won't be of any use unless it's sent an array of buttons to work on. That's the whole purpose of the class, so there's no disadvantage here to having a required parameter. So, we might as well send it what it needs up front.

It's important to realize that once the constructor runs, the class will then have a reference to the buttons that are on the stage in the fla file. They're the exact same set of buttons! The class stores the array locally as "buttons," and in the fla file, it's known as "buttonArray," but both names refer to one and the same array object. In fact, the same name could have been used for both, since they're each in their own different scope.

Next, we're going to write code that loops through the array and adds an event listener to each of the buttons. Add this line to the constructor, right after the buttons = theButtons line:
addListeners();

Next, we'll write the addListeners() function:
private function addListeners():void {
    for(var i:int = 0; i < buttons.length; i++) {
        buttons[i].addEventListener(MouseEvent.CLICK, clickHandler);
    }
}

This function runs through a for loop, which loops through all of the buttons and adds the same event listener to each one. The event to listen for is the MouseEvent.CLICK event. At this point, a little alarm bell should go off in your head: We need to import the MouseEvent class if we're going to use mouse events, so add this line right after the package statement, where the import statements go:
import flash.events.MouseEvent;

Next, we need to write the clickHandler function. At this point, the thought process goes something like this: Inside the clickHandler function, we need to disable whichever button is currently being clicked, that is, prevent it from being clicked again. Secondly, we need to restore the click functionality to whatever button might have been clicked on a previous time. So we need variables to keep track of these things. We'll call the button being clicked "currentButton" and the previously clicked button "lastClicked." Add these variables to the variables list:
private var currentButton:MovieClip;
private var lastClicked:MovieClip;

Now, of course, you'll need to add MovieClip to your list of import statements:
import flash.display.MovieClip;

I mentioned before that we want this class to work with any kind of Button, Sprite, or MovieClip, but for now, we'll type these variables as MovieClip. This will be okay for our intial testing purposes, as the buttons in the fla are MovieClip instances, but later we'll abstract it some more to include the other types. That way, I can save that explanation for later on, too. Here's the clickHandler function:
private function clickHandler(event:MouseEvent):void {
    if(lastClicked != null) {
        lastClicked.mouseEnabled = true;
    }
    var clickedButton:MovieClip = MovieClip(event.currentTarget);
    currentButton = buttons[buttons.indexOf(clickedButton)];
    currentButton.mouseEnabled = false;
    lastClicked = currentButton;
}

The first thing this code does is checks whether lastClicked does not have a null value. Because if the value of lastClicked is null, then the click that we are getting right now is the first click on any button. But if the value of lastClicked is not null, then there must have been a previous click, and the click that we are now getting is either the second click or later. So if there was a previously clicked button, it was also formerly disabled, so we want to restore it to being clickable again.

The next thing the code does is detects which button received the click, and this is stored in a temporary local variable called "clickedButton." The next line sets the value of currentButton equal to whichever button in the buttons array is the one that was clicked. It does this by using the array method "indexOf()" to search through the array and determine the index of the button that was clicked. This is kind of a shortcut to looping through the array yourself and comparing each button to "clickedButton." Next, currentButton has its mouseEnabled property set to false, which disables it from being clicked on again. Finally, the value of lastClicked is set equal to the currentButton. This will be the button that will be reinstated the next time one of the group of buttons is clicked on.

Alternatively, you could use the removeEventListener() method to disable the buttons, and then use addEventListener to restore them to being clickable again. That works just as well, and I doubt that there are any performance issues for something this simple, or any reasons for preferring one way over another. To me, it just seems easier to just set mouseEnabled to true or false. It's a lot less typing!

There are a couple of further things needed to be done in this class before we can test it. There needs to be some kind of visual feedback to let us know that a button is disabled or enabled. One visual cue would be the hand cursor that you get when buttonMode is set to true. So go back and add this line to the addListeners() function, inside the loop where the buttons are being assigned the listener:
buttons[i].buttonMode = true;

All of the buttons will now have a hand cursor. But when a button is disabled, it won't, it will have the arrow cursor instead. As a further visual cue, though, let's set the alpha of the disabled button to 50%, and when a button is restored, restore its alpha back to 100% again. Here's the modified clickHandler function with this alpha setting added in:
private function clickHandler(event:MouseEvent):void {
    if(lastClicked != null) {
        lastClicked.mouseEnabled = true;
        lastClicked.alpha = 1;
    }
    var clickedButton:MovieClip = MovieClip(event.currentTarget);
    currentButton = buttons[buttons.indexOf(clickedButton)];
    currentButton.mouseEnabled = false;
    currentButton.alpha = 0.5;
    lastClicked = currentButton;
}

Putting it all together, here is the class listing so far:
package com.mysite.utils {
   
    import flash.events.MouseEvent;
    import flash.display.MovieClip;
   
    public class ButtonDisabler {
       
        private var buttons:Array = new Array();
        private var currentButton:MovieClip;
        private var lastClicked:MovieClip;
       
        public function ButtonDisabler (theButtons:Array) {
            buttons = theButtons;
            addListeners();
        }
        private function addListeners():void {
            for(var i:int = 0; i < buttons.length; i++) {
                buttons[i].addEventListener(MouseEvent.CLICK, clickHandler);
                buttons[i].buttonMode = true;
            }
        }
        private function clickHandler(event:MouseEvent):void {
            if(lastClicked != null) {
                lastClicked.mouseEnabled = true;
                lastClicked.alpha = 1;
            }
            var clickedButton:MovieClip = MovieClip(event.currentTarget);
            currentButton = buttons[buttons.indexOf(clickedButton)];
            currentButton.mouseEnabled = false;
            currentButton.alpha = 0.5;
            lastClicked = currentButton;
        }
    }
}

Go ahead and save this class if you haven't already.

Next, let's turn our attention back to the fla file, and test this out. First, the fla file code needs to import the class we just made:
import com.mysite.utils.ButtonDisabler;

Next, we need to create an instance of the ButtonDisabler class and pass it our array of buttons:
var bd:ButtonDisabler = new ButtonDisabler(buttonArray);

So the complete code listing on frame 1 of the fla file is this:
import com.mysite.utils.ButtonDisabler;
var buttonArray:Array = [b1, b2, b3, b4, b5];
var bd:ButtonDisabler = new ButtonDisabler(buttonArray);

Press CTRL-ENTER to test the movie. When you click on one of the buttons, it will fade to 50% alpha, and the hand cursor will go away. If you then click on another button, the one formerly clicked on will be restored to full alpha and become clickable again, and the newly clicked button will be disabled.

At this point, we can congratulate ourselves on having made a well-behaved class that we can easily re-use to give any group of MovieClip buttons some button-disabling behavior. However, there are a couple of problems: One of them is that our class is hard-wired to always use MovieClips, and we want the class to be more versatile than that. Later, you might have a project where you want to use this for a group of Button symbols, or even Sprites, or even some combination of these other types. Our class should be able to handle it all!

The other issue is that our class is hard-wired to always make the buttons fade to 50% alpha and then be restored to 100% alpha. That limits you to only one effect! What if, in another project, the effect you want is different. For example, you might want a button to scale to one and a half times its size when it's disabled, then go back to normal size when it's restored. Or some other similar effect. Or maybe, no effect at all.

Not only that, but if we distribute this class for others to use, they may want to use it in these other ways, too. But, ideally, nobody should be required to go in and edit this class again, it's just not good OOP! Clearly, we have a bit more work to do on this class! And we'll take on these other issues on the next page!