Building Your Own Flash MX Components
Building Your Own Flash MX Components
Building Your Own Flash MX Components
Annotated presentation, downloads, and sample chapters from my two books (Sams Teach Yourself Flash MX in 24 Hours and ActionScripting in Flash MX) available at: www.phillipkerman.com/devcon02/ __Overview Components are not complex and elaborate code snippets that someone else built and installed in Flash MX (or uploaded to the Macromedia exchange site). Not necessarily. You'll see they don't have to be complex. They don't have to be elaborate. And, they're easy enough for anyone to build.
Building your own Flash MX Components Macromedia DevCon 2002 Phillip Kerman www.phillipkerman.com [email protected]
One difference between built-in properties and variables in clips is that built-in properties are set during authoring. Variables in clips are set during runtime with ActionScript. Setting variables is not difficult, but if you want the variables in each instance of the same symbol to start with different values you must set the variables from outside the clip. That is, initializing clip variables individually is achieved with a frame script outside the instance or in a load clip event script attached individually to each clip instead. (Actually, it's possible to write a dynamic expression and place it inside the master symbol perhaps in its first keyframe that initializes variables uniquely based on a clip property such as _name but you'll see Components make this unnecessary.) The practical problem initializing variables outside the master symbol is that you either need to name each instance (so you can refer to the variable contained) or you need to place a separate (load clip event) on every instance. For every new instance you need to remember to either name it and set its variables or to attach a script to the individual instance. Therefore, your code becomes spread out and repeated in many places. Components solve this issue by allowing you (the author) the ability to specify which variables (call them properties or parameters) can be set via the Parameters tab of the Properties panel by the author using the Component. In this way, you now have homemade properties that are changed via a panel.
Building your own Flash MX Components Macromedia DevCon 2002 Phillip Kerman www.phillipkerman.com [email protected]
Building your own Flash MX Components Macromedia DevCon 2002 Phillip Kerman www.phillipkerman.com [email protected]
In addition, you can select from nine "types" which are comparable to data types. Basically, the type you choose affects how the using author will populate the variables. Here are the nine types: Default is for individual numbers or strings. Using authors can basically set values to anything they want. If you want to force a String or Number or Boolean data type, use those selections. List lets the author select from a popup menu. This way you limit the values from which the using author may select. In the end, your variables still contain just one string or one number value. Array is quite different than List. Not only can the using author add and remove items (where in the List type is a predefined list) but in the end, the variable becomes an array data type with individual values in each index. Object lets the using author set values for multiple properties of a single variable. The generic object data type (also known as "associative arrays") is like regular arrays with their multiple values, however objects have a name for each value. It's really like having a set of name/values within a single variable. This option is best when you need or want simple objects in your clip. It takes more effort to populate this type of variable but you can restrict the using author to a predefined list of properties like the List type. The two new cool features are Font Name and Color. Font Name provides a list of installed fonts and sets the corresponding variable to a string version of that font name. Color presents a standard Flash color picker and sets the variable to the integer version of that color. (Use someNumber.toString(16) to convert it to a hex string if you prefer.) You can allow the using author to rename, add, or remove the parameters you defined by un-checking "Parameters are locked in instances". You can also provide a description that serves to explain how to use the Component. Realize that the using author sets variables that are part of the main Component. You can use as many variables both contained in the Component and in nested clips but through the Parameters tab of the Properties panel the using author simply sets variables of the Component. Only make the using author populate the variables necessary to make the Component behave uniquely.
Building your own Flash MX Components Macromedia DevCon 2002 Phillip Kerman www.phillipkerman.com [email protected]
__Additional Information
Tip 1: Buttons can't be placed on top of buttons. While it would be very convenient to use the on (rollOver) mouse event that a button offers, if you plan to use your Component on top of buttons inside a movie you'll find the two buttons conflict. For example, if you make a custom cursor Component that lets the using author place it on top of any other button, you cannot use an invisible button inside the Component (to figure out when it's time to show the cursor). Instead use either the getBounds() or hitTest() methods. Consider this code snippet:
onClipEvent (load) { rect=this.getBounds(_parent) _visible=false; } onClipEvent(mouseMove){ if(_xmouse>rect.xMin&&_xmouse<rect.xMax&&_ymouse>rect.yMin&&_ymouse<rect.yMax){ active=true; } else { active=false; } }
Using hitTest() may look cleaner, but notice that since you can't make the clip itself invisible you'll need to make its alpha 0:
onClipEvent (load) { //_visible = false; _alpha = 0; } onClipEvent (mouseMove) { if (hitTest(_root._xmouse, _root._ymouse, 1)) { active = true; } else { active = false; } }
Tip 2: If you plan to use attachMovie() to dynamically create clip instances inside your Component, be sure to include a copy of each symbol (being attached this way) in a guide layer. That way, when you copy the Component to another file, all the associated symbols are copied with it. Naturally, the guide layer isn't needed during runtime it just assures all the symbols get copied.
Building your own Flash MX Components Macromedia DevCon 2002 Phillip Kerman www.phillipkerman.com [email protected]
Tip 3: You can override built-in properties by including them when you set up the Component Definition. For example, specify that _name will be set through the Parameters tab. If you provide a default this makes it possible to automatically name every instance that's dragged on stage. By the way, this will override any settings you make through other panels such as the Properties panel. Tip 4: When you add parameters (through Define Clip Parameters), instances on stage may need to be replaced to reflect the new options.
Building your own Flash MX Components Macromedia DevCon 2002 Phillip Kerman www.phillipkerman.com [email protected]