Tutorial details:
Written by: Guy Watson , MX 101
Time: 40 minutes
Difficulty Level: Intermediate
Requirements: Flash MX
Topics Covered: Dynamic applications of Macromedia's built-in Flash MX Components.
Assumed knowledge: Variables, Pathing, Objects, Component Basics, Scripted Loops.

Its pretty straight forward for any flash user to drag a component onto the stage from the components panel and change its parameters. What if you want to attach one of the components onto the stage with actionscript, during your movie and have it function as it does when it is simply dragged on the stage?

Lets take the Flash MX Scrollbar Component as an example. We want to be able to attach any number of scrollbars onto the stage, whenever we need one, with actionscript.

So, open flash and create a new flash movie.

Open the components panel and make sure you have the 'Flash UI Component Set' open, if you dont, then select it from the list in the top right hand corner of the panel.

Drag an instance of the 'ScrollBar' component onto the stage. This will place all the symbols the component requires to function correctly in your movies library.

Now we move to your movies library, select the main symbol for your component in your movies library, in the case of this example, the component symbol is named 'ScrollBar' and is located in the 'Flash UI Components' folder as are all, Official Macromedia Components. Right-click the selected symbol and choose 'Component Definition' from the menu. A panel will open, in the 'Parameters' field, you will see listed, all of the parameters that the selected component symbol in your library accepts, in our example, you should see that the 'ScrollBar' component requires to parameters to function correctly, Target Textfield and Horizontal.

Now, with the knowledge that this component, the 'ScrollBar' component, requires the parameters listed in the 'Component Definition' panel to function correctly, we are well on our way.

Parameters have a 'Name' and a 'Variable' associated with them, the 'Name' is basically an identifier which is meant to be descriptive of the value you will be entering, so for example Target TextField, tells the user that will be entering a Textfield name of some kind. The 'Variable' is the name of the actionscript variable that will be placed inside of the component symbol instance when the movie begins to play, along with the associated value that the user of the component enters in either the 'Component Parameters' panel or the 'Properties Inspector' panel.

The 'Variable' is what we will be needing to make our Components function correctly.

As i said previously, there are two Parameters that the 'ScrollBar' component requires to function correctly, because we wont be able to enter the values for these parameters, when we attach the component symbol, from our library onto the stage, with actionscript, we need to know the variable names for each of these parameters. A quick look at the 'Component Definition' panel, tells us that the variables are _targetInstanceName and horizontal.

Now we need to know, what type of value, these two parameters are, we dont want to pass a string as a parameter when it is expecting a number. To find this out we look at the 'Type' attribute for each parameter. In the case of our example, the Target Textfield should be a string, and the Horizonal parameter should be a Boolean Value, true/false.

We now have all the information we need to attach our component dynamically onto the stage with actionscript, there is just one more thing we need to know and that is the 'Linkage Identifier' for the component symbol. To find this out, you need to select the component symbol in your library again, in this example the symbol name is 'ScrollBar' and as i said previously, it is located in the 'Flash UI Components' folder. You then need to right-click and choose 'Linkage' from the menu. Look in the 'Identifier' field and you will see a string, which we will be needing in a few moments, in this example, it should say 'FScrollBarSymbol' this 'Linkage Identifier' is based upon standards/best practices which Macromedia are trying to preach/set, which you can read more about here.

Now lets get down to the final stage. To attach a component onto the stage with actionscript, you need an instance of the component in your library along with all the associated files, you need to know the 'Linkage Identifier' for the component symbol and you need to know the 'Variable' names for the parameters the component requires.

To attach our component onto the stage we will use the Movieclip.attachmovie method, this method requires three arguments and has one extra optional argument, which this process requires. To attach a symbol onto the stage the attachmovie method needs to be given, the 'Linkage Identifier' for the component symbol, an instance name which will be assigned to the symbol instance and a depth to place the new movieclip on. In Flash MX, you can now pass a fourth, optional parameter, which is an object, containing properties which you want to be copied over to the the new Movieclip Object when it is created.

As you should now know, all components require certain parameters to function correctly, these parameters need to be defined inside of the component symbol before the Constructor of the component class is executed, we will therefore use the fourth argument of the Movieclip.attachmovie method to initalize our component instance with the variables it requires to function properly.

Continued overleaf...