XML: Yes, It Actually Has a Purpose
All the behaviors in Flash MX 2004 are created via a .xml file in your install directory. I know, I know: XML is a bit of a weird concept. If you're like me, you heard everyone saying that it was the wave of the future, so you spent five minutes looking at how it's written and never really found a purpose for it. Later, I realized that Flash MX 2004 is extensible, and I must finally put XML to work to extend it. And I'm glad I did.
Okay. Enough ranting. Let's write some code.
Open up your favorite XML editor. You can use anything from Notepad to Dreamweaver to do this, so just choose a program you like.
Choose File > Open, locate HideScreen.xml in your Flash install directory, and open it.
Windows: C:\Documents and Settings\your user name\Local Settings\Application Data\Macromedia\Flash MX 2004\en\Configuration\Behaviors
Mac: Macintosh HD/Users/your user name/Library/Application Support/Macromedia/Flash MX 2004/en/Configuration/Behaviors/
Perusing this file a bit reveals how it works. The first couple of lines define the version numbers of the XML and flash_behavior. The first major section of code (after the copyright information) is the behavior_definition section, in which you tell Flash what category the behavior goes into and its name. (Here, you can also declare a dialog box ID if one is needed.)
After this section is the code that creates the dialog box used in the Hide Screen behavior. That's right: You can create your own dialog box for a behavior and use it to customize the resulting script. Use this file as an example if you ever need to do this. (For this behavior, which will hide the contextual menu in a SWF, you don't need a dialog box.)
The last section provides the ActionScript that is created by running this behavior in Flash. In short, to create a custom behavior, you need to know the ActionScript used to create the functionality and then set up an XML file to create it automatically. So let's get to it.
Choose File > Save As and save this file HideContextMenu.xml into the same directory it came from (the Behaviors folder in your Flash install directory).
Delete the copyright information.
In the behavior_definition section of the XML, delete the dialogID and screenModeOnly lines. You don't need them.
For the category parameter, replace "Screen" with "Context Menu". This creates a category in the Behaviors panel called Context Menu, under which the behavior will be found.
For the name parameter, replace "Hide Screen" with "Hide Context Menu". This parameter defines the name that will be listed in the Behavior panel.
Delete everything from the opening <properties> tag to the closing <dialog> tag. All this stuff generates a dialog box, and you don't need one for this behavior.
In the <actionscript> section, delete the ActionScript and change the first comment (which begins with two forward slashes (//)) from "Hide Screen Behavior" to "Hide Context Menu Behavior".
Now that the code has been stripped of the elements you aren't using, you just need to add the ActionScript that hides the context menu to finish the file.
Add the following code between the opening and closing comments in the <actionscript> section:
Save your work.
var newMenu = new ContextMenu(); newMenu.hideBuiltInItems(); this.menu = newMenu;
This script creates a new ContextMenu object, hides its built-in menu items, and assigns the object to the main timeline. If you've followed along, the completed XML file should contain the same code as shown in Figure 2.
Figure 2 XML: Another language to add to your resume.
The last thing to do is test it out.