Projects
Example 4 - Buttons Generated from Array
Intro : Example 1 : Example 2 : Example 3 : Example 4
Library
- button - Movie Clip: exported from library as "button"
- content - Movie Clip
- invisible - Button
Construction
- Button movieclip is built the same as previous examples
- No buttons are placed on stage. Content movieclip is in place with instance name of "content"
- Frame 1, actions layer, creates all the instances of the button and assigns values to the variables
Actionscript
Attached to invisible button on button movieclip timeline
on(release) {
_parent.content.gotoAndStop(frame);
}
Attached to frame 1
buttonNames = new Array("Home", "Services", "Products", "Advantages", "Process", "Contact");
buttonHeight = 23;
for (i = 0; i < buttonNames.length; i++) {
_root.attachMovie('button', 'button'+i, i);
_root['button'+i]._x = 0;
_root['button'+i]._y = i*buttonHeight;
_root['button'+i].text = buttonNames[i];
_root['button'+i].frame = i+1;
}
stop();
- In the first line, we create an array with all the button labels
- Line 2 sets the spacing of the buttons
- Lines 3 - 9 loop through each element of the array
- Line 4 attachs an instance of the button movieclip to the main timeline, giving it an instance name of 'button'+i, and a depth of i
- The next 2 lines set its position by modifying the _x and _y properties. We're dynamically grabbing the instance name: _root['button'+i].
- The following 2 lines set the text and frame variables. If our content frames weren't sequential, we could easily set up another array holding their values
Advantages
- Extremely easy to customize. Simply add an element to the array and a new button is automatically created. Just need to add corresponding content to content clip
- Could be made much more dynamic with little work: could change to button action to load a new movie clip into content, etc.
Disadvantages
- Very abstract - you don't see the layout in the authoring tool
Recently Played on iTunes
-
“Heroin”
The Velvet Underground & Nico
The Velvet Underground
11/17/08 16:26 -
“All Tomorrow's Parties”
The Velvet Underground & Nico
The Velvet Underground
11/17/08 16:20 -
“Run Run Run”
The Velvet Underground & Nico
The Velvet Underground
11/17/08 16:16