16.8. Packaging Components as Smart Clips
A Smart Clip is a movie clip that allows some of its variables to be assigned through a special graphical user interface in the Flash authoring tool. Smart Clips allow non-programmers to customize programmatically-controlled movie clips. Smart Clips separate the behavior-determining variables from the code of a clip, which lets people treat them as "black boxes" -- their operation can remain mysterious as long as their inputs, outputs, and behavior are known.
// User-defined variables var numSparks = 10; // Number of spark clips in the explosion var randomDispersion = true; // Explosion style (true for random, // false for uniform) var duration = 1300; // Length of explosion, in milliseconds
Modifying source code of this sort can be intimidating for non-programmers. But if we build our system as a Smart Clip, non-programmers can configure the fireworks effect through a familiar application-style interface. Figure 16-2 shows a Smart Clip interface equivalent to our variable-initialization code.
Figure 16-2. A sample Smart Clip -configuration interface
In a Smart Clip interface, each variable appears with its name and value clearly distinguished in separate rows and columns. Variable names cannot be edited, so there's no chance of inadvertent typos breaking the system. Each variable also has its own verbose description explaining exactly what it does and how it should be set. Finally, variables with a limited set of legal values (such as randomDispersion) may be assigned via drop-down menus.
For non-programmers, the interface shown in Figure 16-2 is certainly more approachable than source code. But Smart Clips can actually be made even more user friendly. We may replace the default Smart Clip interface with our own custom interface, such as the one shown in Figure 16-3. Notice how the custom Smart Clip interface hides our system's variables entirely, allowing the non-programmer to tailor each instance of the fireworks effect with text fields and pull-down menus. The interface even provides a live preview of the effect in action!
Figure 16-3. A customized Smart Clip-configuration interface
Let's see how all this works.
16.8.1. Building a Smart Clip with a Standard Interface
The first step in building any Smart Clip is creating a regular movie clip that is controlled by the value of one or more variables. In the following code, for example, the variables xPos and yPos determine the location of a clip on stage:
_x = xPos; _y = yPos;
When we build a movie clip as a Smart Clip for ourselves or someone else to use, we expect certain designated variables to be set via the Smart Clip interface when the clip is placed on stage. Those variables are known as clip parameters. Once we've created a clip with behavior dictated by one or more clip parameters, we must give the clip a Smart Clip interface through which those parameters can be set.
22.214.171.124. Adding a standard interface to a Smart Clip
To add a default Smart Clip interface to a movie clip, follow these steps:
126.96.36.199. Configuring standard clip parameters
After we add a clip parameter to a Smart Clip, we must assign the parameter a name and, optionally, a default value. Like variables, clip parameters can contain different types of data. The datatypes supported by clip parameters are, however, not quite the same as those supported by variables. Clip parameters may contain strings, numbers, arrays, objects, and lists. These differ from the datatypes supported by variables in two ways:
To give a clip parameter a name and optional default value, follow these steps:
188.8.131.52. Removing and reordering standard clip parameters
To remove a clip parameter, follow these steps:
To rearrange clip parameters, follow these steps:
16.8.2. Building a Smart Clip with a Customized Interface
To build a Smart Clip with a customized interface, we first create a regular movie clip whose behavior is governed by a series of clip parameters as described earlier. Next, we create an independent .swf file (the so-called interface .swf ) that will be used as the Clip Parameters panel interface. We'll typically create a .swf file with a graphical interface that allows the user to enter parameter values (via text boxes, menus, buttons, etc.). Those values are automatically collected and passed to the Smart Clip as parameters.
The Smart Clip communicates with the interface .swf via the xch instance (short for exchange), a specially named instance in the interface .swf. (We'll see how to create the xch instance in a minute.) Figure 16-4 shows how parameter names and values are sent from the interface .swf to the Smart Clip.
Figure 16-4. Custom Smart Clip communication
Communication between the interface .swf and the Smart Clip occurs in a cycle. When a Smart Clip instance is selected on stage, the corresponding interface .swf loads into the Clip Parameters panel. The current parameters in the Smart Clip instance are then passed to the .swf file's xch instance. The .swf file is expected to retrieve those parameters and set the interface state accordingly. Subsequent variables set in xch by the .swf file are automatically passed to the Smart Clip as parameters. When the Smart Clip instance is unselected, the interface .swf is removed from the Clip Parameters panel. However, the parameter values are not lost, they are retained by the Smart Clip. Each time the Smart Clip instance is selected, it passes its parameters back to the .swf file's xch clip. This cycle allows the interface .swf file to stay synchronized with the Smart Clip parameters.
The following sections explain how to create the custom interface .swf and associate it with a Smart Clip. A sample Smart Clip with a customized user interface is available under "Playhead Control" from the online Code Depot.
184.108.40.206. Creating a custom interface .swf file
220.127.116.11. Adding a custom interface to a Smart Clip
16.8.3. Using Smart Clips
To use a Smart Clip instance in a movie, follow these steps:
Copyright © 2002 O'Reilly & Associates. All rights reserved.