10 Minutes with Flash: Writing Custom Behaviors
Flash MX 2004 introduced us to the world of behaviors: ActionScripts that get written for you via the Behaviors panel. Macromedia seems to think that the Behaviors panel is a good replacement for the now extinct Normal mode (which had a similar function and a lot more options). Whether or not you agree, the Behaviors panel can in fact offer a viable alternativeif you know how to write XML and are willing to create your own behaviors.
What's that? You don't know how to write XML? Well, the good news is that you can still create your own behaviors and save yourself lots of coding time by modifying existing behaviors to create your own. In this installment of the 10 Minutes with Flash series, you'll create a behavior that hides the contextual menu in a published SWF. After you have a handle on how it's done, you'll be better-equipped to turn that pesky Behaviors panel with its incredibly short list of possible functions into something more useful.
The Behaviors What Now?
Like many people, you may not have noticed the Behaviors panel while looking through the list of new features in Flash MX 2004. And I don't blame you. It doesn't appear at first to be one of the more groovy happenings in the world of Flash development. But alas, it is. Let's take a quick look at what it does.
Put any ol' JPG onto your desktop and name it test.jpg.
Launch Flash MX 2004 or Flash Pro and save a new document as test.fla to your desktop.
Choose Window > Development Panels > Behaviors to open the Behaviors panel, shown in Figure 1.
On Layer 1, draw a box on the Stage and convert it to a movie clip. Assign it an instance name of container_mc using the Property inspector.
Add a new layer to the timeline called actions.
Select frame 1 of the actions layer and click the Add Behavior button in the Behaviors panel; then choose Movie Clip > Load Graphic.
In the resulting Load Graphic dialog box, enter test.jpg as the URL to the graphic and choose container_mc as the movie clip in which to load the graphic.
Run a test movie. Your test.jpg image should load into the test movie.
Close the Preview window.
Open the Actions panel. Notice that the following code is attached to frame 1 of the actions layer:
Figure 1 The Behaviors panel, just dying to be used.
//load Graphic Behavior this.container_mc.loadMovie("test.jpg"); //End Behavior
Cool, huh? You chose a script from a menu and Flash wrote it for youwith commentsto identify its purpose and separate it from the pack of code blocks that will eventually fill up your Actions panel. Granted, this isn't the most exciting script in the world, but hey, you didn't have to write it yourself, so you probably saved at least five seconds. If you didn't know how to load an external graphic at all, this likely saved you a lot of time.
Now that you're familiar with what behaviors can do for you, you can write one of your own.