Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Digital Audio, Video > Adobe Flash

10 Minutes with Flash: Writing Custom Behaviors

  • Print
  • + Share This
Don't you wish Flash MX 2004 came with more built-in behaviors? 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 only 10 minutes or less, Robert Hoekman, Jr. helps you create your own behaviors and save yourself lots of grief.
From the author of

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 alternative—if 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.

  1. Put any ol' JPG onto your desktop and name it test.jpg.

  2. Launch Flash MX 2004 or Flash Pro and save a new document as test.fla to your desktop.

  3. Choose Window > Development Panels > Behaviors to open the Behaviors panel, shown in Figure 1.

  4. Figure 1Figure 1 The Behaviors panel, just dying to be used.

  5. 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.

  6. Add a new layer to the timeline called actions.

  7. Select frame 1 of the actions layer and click the Add Behavior button in the Behaviors panel; then choose Movie Clip > Load Graphic.

  8. 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.

  9. Click OK.

  10. Run a test movie. Your test.jpg image should load into the test movie.

  11. Close the Preview window.

  12. Open the Actions panel. Notice that the following code is attached to frame 1 of the actions layer:

  13. //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 you—with comments—to 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.

  • + Share This
  • 🔖 Save To Your Account