Dynamic Buttons

I l @ ve RuBoard

Dynamic Buttons

Another way to make pull-down menus and similar things is with dynamic buttons. If you open the example movie 16dynamicbuttons.fla, you can examine the Stage and observe that nothing at all is on it ”not a single movie clip or button.

However, when you run the movie, three buttons appear. If you roll over one of the buttons, several more appear below them.

These buttons were all created dynamically by ActionScript. All that is in the Library is a movie clip and a button. This movie clip is used as the template for all the buttons created by ActionScript at runtime.

To use this technique, you first need to make your button template. I started off with a simple button, including an over and down state.

Next , place this button inside a movie clip. The movie clip should contain two elements: the button and a dynamic text field floating above it. The dynamic text field should be linked to the variable buttonLabel .

Select this movie clip in the Library and set its Linkage properties to Export for ActionScript and its Linkage name to buttonMovieClip.

Now you have a button template that can be used by ActionScript to create new buttons. Creating a new button is just a matter of using attachMovie to create a new instance of the movie clip, setting buttonLabel in the movie clip to change the text on the button, and positioning the movie clip by setting its _x and _y properties.

Here is a handler that does all this. It makes a copy of the movie clip in the Library, sets its label and location, and then returns a reference to that movie clip:

 function createButton(buttonLabel, x, y) {     this.attachMovie("buttonMovieClip","button"+buttonLevels,buttonLevels);     bmc = this["button"+buttonLevels];     bmc.buttonLabel = buttonLabel;     bmc._x = x;     bmc._y = y;     buttonLevels++;     return(bmc); } 

You can test this script in the movie 16dynamicbuttontest.fla. The code there creates one button with the label Test Button.

You can create a whole series of buttons by using multiple calls to createButton . Or you can store the names of buttons in an array and use a for loop to call createButton with the name of each button. We'll look at that technique in the next task.

Another problem that needs to be dealt with is how to get different buttons to react differently. The script in each movie clip's button must be the same, so placing a gotoAndStop command in there means that all the buttons act the same. However, if you have the button call a function at the root level, that function can figure out which button called it and perform an action specific to that button.

I l @ ve RuBoard

Sams Teach Yourself Flash MX ActionScript in 24 Hours
Sams Teach Yourself Flash MX ActionScript in 24 Hours
ISBN: 0672323850
EAN: 2147483647
Year: 2002
Pages: 272

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net