Using the Behaviors Panel
Some Flash developers and designers don't give a hoot about OOP and only use ActionScript 2.0 for relatively minor chores. If you're among that number, you'll find the Behaviors panel in Flash to be just the tool you've been waiting for. It provides built-in code for non- programmers so that all you need to do to get the code you want is to select the task you want accomplished, and the Behaviors panel will whip up the code you need in no time at all.
Project: Loading External JPEG Files into a Movie Clip with the Behaviors Panel
The code from the Behaviors panel can be placed in buttons, movie clips, or frames. Depending on what you've selected when you begin using the Behaviors panel, you will see different selections. To get started, the following steps show you how to create a document that will load external JPEG files into a movie clip. Before you get started, though, create three JPEG files with the names ad1.jpg, ad2.jpg, and ad3.jpg. (In the example, old-time advertisements are used; hence, the name "ad#.jpg" is used.) Here are the steps to follow:
-
Open a new document and create three layers, naming them, from top to bottom, Buttons, MovieClip, and Background.
-
Add the desired background in the Background layer. In the MovieClip layer, use the rectangle tool to create a 50x50 square. Select the square, press F8, and change it into a movie clip symbol. Select the movie clip on the Stage; in the Instance Name box in the Property Inspector, type the name jpgLoader_mc.
-
Select the Buttons layer and create a button by drawing a circle, pressing F8, and selecting Button in the Symbols dialog box.
-
Open the Behaviors panel, select the button on the Stage, and click the add (+) button. When you click the add button, you'll open the Behaviors menu. Select the Movieclip menu and then the Load Graphic option in the submenu, as shown in Figure 3.2.
-
When you select the Load Graphic option, the Load Graphic dialog box opens. In the first text box, type the name of the first JPEG file you want to load (ad1.jpg, in this example). Then select the jpgLoader_mc icon and make sure that the Relative radio button is selected. Figure 3.3 shows what this dialog box should look like when correctly filled out.
-
Select the button and open the Actions panel by pressing the F9 key. Notice that a script has been written for you. Figure 3.4 shows what it should look like.
-
Add two more buttons and repeat steps 4 and 5 so that the remaining two JPEG files can be loaded. Label the buttons appropriately. Figure 3.5 shows what your completed movie will look like when you run it and click one of the three buttons.
Figure 3.2 Selections available from the Behaviors panel's Movieclip submenu when a button is selected.
Figure 3.3 The Load Graphic dialog box.
Figure 3.4 The Actions panel with code written by the Behaviors panel.
Figure 3.5 A different JPEG file loads into the movie clip as each button is clicked.
Project: Loading an MP3 with the Behaviors Panel
The scripts created by the Behaviors panel that go into frames are a different set of selections, and a more limited set as well. However, that doesn't mean you won't be given some terrific code.
The Behaviors panel has fewer selections for scripts associated with frames because both buttons and movie clips have far more events associated with them. For example, a button recognizes events such as being pressed and released, mouse rollovers and drag-overs, and whether the event is on or off the button. A frame essentially has the event of something entering the frame. Nevertheless, some great scripts can be generated automatically for frames. This next example is quite simple, involving a single frame and script. Before starting this example, get a musical MP3 file and name it class1.MP3. Place the MP3 file in the same folder where you'll be saving your FLA file. Here are the steps to follow:
-
Open a new Flash document and click the first frame.
-
Click the add (+) button on the Behaviors panel and select Sound, Load Streaming MP3 file. In the resulting dialog box, type the name of the MP3 file and a reference name (with no spaces). Figure 3.6 shows what the dialog box should look like prior to you clicking the OK button.
Figure 3.6 The Load Streaming MP3 File dialog box.
That's it. You're done. The MP3 file has now been imported into your movie, which you can check by looking in the library and the Sound menu. That is important because your movie will load considerably faster, and the sound will be "streamed" (actually a progressive download) into your movie as the user starts playing it.
To set up the streaming MP3 file, the Behaviors panel generated the code shown in Listing 3.3.
Listing 3.3 MP3 Streaming Code Generated by the Behaviors Panel.
//Load Streaming mp3 behavior if (_global.Behaviors == null) { _global.Behaviors = {}; } if (_global.Behaviors.Sound == null) { _global.Behaviors.Sound = {}; } if (typeof this.createEmptyMovieClip == 'undefined') { this._parent.createEmptyMovieClip('BS_classic_1', new Date().getTime()-(Math.floor((new Date().getTime())/10000)*10000)); _global.Behaviors.Sound.classic_1 = new Sound(this._parent.BS_classic_1); } else { this.createEmptyMovieClip('_classic_1_', new Date().getTime()-(Math.floor((new Date().getTime())/10000)*10000)); _global.Behaviors.Sound.classic_1 = new Sound(this.BS_classic_1); } _global.Behaviors.Sound.classic_1.loadSound("class1.mp3", true);
You need not know what the code means, nor do you have to learn how to code. All you need to do is to select the correct options in the Behaviors panel, and you'll accomplish the necessary tasks. (Your clients don't need to know you can't code!)