Setting up the fla file with the buttons
My interest in Flash started mostly because of a Jib-Jab cartoon ("This Land") in 2004. I'm the author of a feature I call "Mazoons," which are a combination of mazes and cartoons. In 2002, I even had a book published, "Super Silly Mazes." I'm not a professional programmer, but making my mazes interactive by programming them with Flash became a hobby/obsession of mine, to the point where I have now learned more than I bargained for. Lately I'm working on a new website about Flash and Actionscript 3.0 called The Flash Connection.View all articles by Jody Hall
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:)