You may be familiar with this scenario: You have a group of buttons. You want to have them behave so that if you click one of them, it becomes the active button, which won't allow you to click on it again until after you've selected another. Each time you click a button, it becomes the currently selected button, and the previously selected button becomes clickable again. This has many applications--for example, it could be used for a website: You might have four or five buttons representing your different web pages, like "Home," "About Us," "Products," "Links," and "Contact." If you're currently on the "About Us" page, you want to disallow another click on the "About Us" button, because you're already there. You may have even coded this before. And if you've coded it more than once, well, that just might be a clue that maybe this would be a good candidate for a reusable class.

I actually just recently thought up the idea for this class myself, because I thought it would be a handy class to have. Then when I wrote it, I was really happy with it, and only after the fact did I start thinking it might make a cool subject for a tutorial. So, it still being fresh in my mind, I'm going to take you from the initial concept to the finished product in short, easy steps. Here we go! Ready?

So the goal here is to take a set of buttons, send them to the class, and the class will give them the button disabling behavior described above. First of all I should make clear that many times when I talk about buttons, I usually mean MovieClips being used as buttons, and not  necessarily the Button symbol (or the SimpleButton class). Many long-time users of Flash have realized that MovieClips can do anything Buttons can do, and so much more. Anyway, in the succeeding discussion, when I say "button" (lowercase b), it can sometimes mean MovieClip or Sprite being used as a Button. But if I say "Button" (uppercase B), it will always refer to the Button symbol.

However, the class that we're going to write here really should be able to handle any group of buttons, whether they're Buttons, MovieClips, Sprites, or even a mixture of those. I think when you write classes, you go through a kind of mental exercise where you try to abstract things and make your class as versatile as it can be. It's almost as though you're programming for other programmers! You picture in your mind how you or others are going to be using your class, and you want to make it as easy to use as possible. Not only that, but you want to make it generic enough so that nobody will ever have to go in and modify it.

Start Flash, and open a new Flash File (Actionscript 3.0). Draw Something on the stage. A small circle or an oval will do. Select it, and press F8 to convert it to a symbol. Give it a library name of "Btn," and choose MovieClip as the type. Place the registration point in the center. Click OK. Give the instance on stage an instance name of "b1." Drag out four more instances from the library and place them to the right of the first one. Give these other buttons instance names of b2 through b5 (and remember what I just said about "buttons"--I'm actually talking about MovieClip instances here, of course).

Now you should have a row of buttons, with instance names of b1 through b5. We're keeping it simple for this example; the buttons don't have any text on them, for example. But really, the buttons you use can be as elaborate as you want. Our class's code isn't going to care what the buttons look like, or how they're positioned on the stage, or how many of them there are, or anything like that, it's only going to concern itself with giving them a certain behavior. This will help make it all the more reusable.

The code that creates an instance of the class we'll write could be placed in a document class. But I want to stay focused on the ButtonDisabler class and not confuse anyone or get side-tracked. So we'll just place the code in the fla file's timeline, on frame 1 of an actions layer. So, add a new layer to the timeline and name it "actions." Click on the first frame, and press F9 to get the actions panel. The first order of business is to list the buttons in an array, so type in this code:
var buttonArray:Array = [b1, b2, b3, b4, b5];

You can save this fla file as "button_disabler_test.fla" anywhere on your hard drive. We'll leave this file for now, but you should leave it open in Flash so that you can come back to it. I know we didn't write any code other than to list the buttons in an array, but at least that gets us thinking about the buttons as a group, and it's this array that will eventually get sent to our class, which we will write next. On to the good stuff!

(Here's a sneak peek at the button behavior I described, just to give you an idea of what we're making here:)