Programmable Buttons

[ LiB ]

Programmable Buttons

As early as Chapter 1, you created your very first button. You learned all about the Up, Over, Down and Hit states. The button you created even reacted to your mouse positionif you were over it, it displayed the Over state;if you clicked it, it displayed the Down state; and so on. We had one issue though: That was the extent of the button's functionality. In this section, I will explain how to program any button that you might need.

Flash has a Common Library that contains many stock symbols such as buttons, graphics and sound effects and they allow you to save time and get to your programming right away. This stored-away Common Library can be accessed through the Windows menu.

Click on Windows, Common Library, Buttons. The Library window, shown in Figure 4.1, will open .

Figure 4.1. Common button library

graphic/04fig01.gif


Remember that to use something from the library, you have to drag it to your workspace. From the Library window menu, select Arcade buttons, arcade button orange. Go ahead and drag it to the stage. This little shortcut just allowed you to skip the whole process of making a button yourself; now let's move on to the programming process.

Select the button on the stage with the Selection tool. Either right-click it and select Actions or just press F9. Now that you have a blank screen in front of you, go ahead and set it up so that every time you click on the button, you load an external Movie Clip while streaming audio in the backgroundjust kidding. We'll do that later. For now, make sure you have the Actions Panel open with your button selected and just check out the following listing.

 on (release) {   trace("You have clicked the button!"); } 

You're looking at a new type of code block. This code block is called an on handler partly because it begins with the on keyword (duh!). The on handler can be tricky to understand at first, but it's fairly easy once you get the hang of it. This on handler makes the button execute the code within this block every time the user releases the mouse button (from a click). Makes sense so far?

In other words, the button looks for code that's handling these events. If they are not there, nothing happens. In this case, we defined the on release event. The only time the on release event can happen is right after the user releases the mouse button after initially clicking on your Flash button.

We all know what the trace command doesoutputs information to the Output Window. In this case, when the user releases from his mouse click, a message will be sent to the Output Window. Let's test this out. After typing the listing above into the button's Actions Panel, go ahead and save the file. Remember to name it something friendly and also put it where you can access it easilylike on your desktop.

Test the movie by pressing Ctrl+Enter (or Command+Return). You should get a blank white screen with an orange button in the middle. Click and hold on that button. Notice that nothing happens. Once you release the button, you should get a message in your Output Window. Click and experiment a few times. You should have a screen similar to that in Figure 4.2 once you're done.

Figure 4.2. Testing the on release handler

graphic/04fig02.gif


I have saved the project file as GDA_PROG4.1.fla on the CD. If you don't feel like typing in the long listing in this section, go ahead and play with the project file I named above.

Let's change this on release handler into an on press handler so that you can get a feel for the different events to which you can assign code. See the following listing for the modifications. This program has been saved as GDA_PROG4.2.fla on the CD. Again, try pressing and holding the button and then releasing it while observing all the stages of the code execution.

 on (release) {   trace("You have released the button!"); } on (press) {   trace("You have pressed the button!"); } 

So a block is executed once you press the button and another block is executed one you release the buttonwhat's the big deal? Flexibility! Flexibility is everything when it comes to a computer language and ActionScript is very flexible. Remember that the more control you have, the more you will be able to tell the computer to do.

We'll revisit buttons after the next section on Movie Clips. As I said before, most of this knowledge is circularyou won't completely understand one thing unless you understand the next . There's no need to worry too much about this because I knocked all the difficult stuff out of the way for you.

In the next section you'll see how you can easily control a Movie Clip from a button's handler. It's simple yet powerful functionality. Think of the possibilities when you can assign any functionality you want to your buttons!

[ LiB ]


Game Development with ActionScript
Game Development with ActionScript
ISBN: 1592001106
EAN: 2147483647
Year: 2004
Pages: 162
Authors: Lewis Moronta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net