Learning the Accordion
One of the most useful user interface constructs is the accordion panel. It allows you to stack various sections of your site and then expand and contract the sections when they're clicked on. In this tutorial, we'll create a basic animating accordion panel that can be used as the basis for a full Flash website.
1. Examine the files
Open the accordion folder (from the download files available at www.layersmagazine.com) and you should see two FLA files: The accordion.fla file is what you'll need to open to start the tutorial, and the accordion_final.fla file is a finished version of the tutorial that you can use as a reference. There's also another folder named gs, which contains an ActionScript animation library called TweenLite that we'll use to do our animation.
2. Open the FLA
Open the accordion.fla file in Flash CS4 (this tutorial will also work with Flash CS3 if you haven't yet upgraded). On the Timeline, you'll see two layers: The actions layer will hold all of our ActionScript 3.0 code for this project and the panels layer will contain all of the various sections for our site. The movie is 690 pixels wide and 355 pixels high with a frame rate of 30, and it's set to publish for Flash Player 9, as we won't be using any of the new Flash Player 10 features.
3. Draw a panel
Select the first frame of the panels layer and select the Rectangle tool (R). In the Property inspector, remove the Stroke color (if there is one) and set the Fill color to #0066CC (blue). Drag out a rectangle on the Stage at any size, then in the Property inspector set the following: W(idth) 600; H(eight) 355; X 0; Y 0. (Note: Make sure the chain icon is "broken" next to Width and Height.) Control-click (PC: Right-click) on the rectangle and choose Convert to Symbol. In the dialog that appears, name it "panel1," make sure that Movie Clip is the Type, and click OK.
4. Add a text field
In the Property inspector, give the new movie clip an Instance name of "panel1." Double-click on it with the Selection tool (V) to enter edit mode. Select the Text tool (T), set the Character Family to Myriad Pro (or another font-make sure it's a clean, legible font, as it will be rotated 90°), enter Size (as desired), and Color (we chose white). Click on the Stage and enter the text, "Panel 1." Switch to the Selection tool, open the Transform panel (Window>Transform), and set Rotate to 90°. Return to the Property inspector and set the type's X property to 0 and Y to 9. Click on Scene 1 to return to the main Timeline.
5. Duplicate the panel
Now we'll duplicate the movie clip panel for each section of the site. In the Library panel, Control-click (PC: Right-click) on the panel1 movie clip and choose Duplicate. You'll be prompted to give the new clip a name, so let's call it "panel2." Now drag an Instance of the panel2 movie clip onto the Stage, and in the Property inspector, position it at X 30 and Y 0. Give the clip an Instance name of "panel2." (Obviously, if you were building a real site you could name the various clips based on what they contained.)
6. Modify the duplicate panel
Double-click on the panel2 movie clip to enter edit mode so you can customize this panel. With the Text tool, change the text to "Panel 2." Then, select the rectangle and choose another color in the Property inspector. Go back out the main Timeline. Now follow the same steps to create two more panels. Offset the X property on each panel by 30 pixels-that means the third panel will have an X of 60 and the fourth will be X 90. Be sure to give each panel an Instance name, following the same convention as above.
7. Import tweenlite
Select the first frame in the actions layer and open the Actions panel (Window>Actions). Enter the code shown here into the panel. These two lines of code import the TweenLite animation library so that we can use it. TweenLite is an ActionScript 3.0 library that makes animating with code extremely simple. All that's required is a single line of code to create some really complex animations. There are other engines such as Tweener and gTween that would also work well for this tutorial. (Note: Make sure the gs folder is in the same folder as your FLA file.)
8. Set left and right positions
Add the code highlighted here into the Actions panel. In these four lines of code, we're attaching some information to each of the panels that will help us when we need to animate them. The first property, lx, is the leftmost X position of the panel. The rx property is the X position for the panel when it's on the right side of the movie. Notice how all of these values are offset by 30 pixels from one another. The ind property simply holds the index number of the panel.
9. Add click events
Now we need to set up the click events for each of the panels so that they can react when one of them is clicked. Add these next highlighted lines of code to the Actions panel. The first four lines sets up the click events for each panel. All of them will call a function named onClick that's located at the bottom of the code block. When a panel is clicked, it will animate all of the panels to the correct position allowing the user to view the full contents of that panel.
10. Who was clicked?
Since all of the panels will call the same function when they're clicked on, we need to determine which one was clicked so we can animate the panels appropriately. We can easily get a reference to that clip by using the target property of the event object that gets sent to the function. Add this highlighted code into the Actions panel at the specified location to create a variable named "clicked," which references the clip that was clicked on.
11. Loop through the panels
Now that we know which clip has been clicked on, we need to loop through all of the panels and animate them to the correct position. In ActionScript 3.0, the most common way to do this is by using a "for" loop. A counter variable is incremented each time the loop is run until it no longer satisfies the condition. Enter the highlighted code into the Actions panel at the specified location. Inside the loop we create a new variable named "mc" that references the panel clip based on the value of the loop counter i.
12. Animate the panels
Enter this highlighted code into the Actions panel at the location specified. First, we check whether the index of the current clip is less than or equal to the index of the clip that was clicked on. If it is, we need to animate it to the left. If not, it needs to be animated to the right. For each case, we're using the TweenLite.to function to do the animation. The animation length is set to 0.5 seconds and we're using an exponential ease-out effect. Both of these settings can be customized to change the feel of the animation (see Step 14).
13. Add content to the panels
Test the movie by choosing Test Movie from the Control menu. Click on the various panels to see the nice animation effect. Now that the code is complete, you can start customizing the panels so that they contain actual content. Double-click on one of the panels to enter edit mode. Now you can start adding text, images, video, or anything else that you want on that particular panel. Check out the accordion_final.fla file to see an example of a finished panel.
14. Customize tweenlite
As mentioned in Step 12, you can change some of the values in the TweenLite function call to customize the animation effect. Try changing Expo.easeOut to Bounce.easeOut. This makes the panels bounce into place. Another interesting choice would be Elastic.easeOut, which gives it a springy, elastic effect. You can also adjust the length of the animation to achieve different results. To see the full list of customization options, check out the documentation for TweenLite at http://blog.greensock.com/tweenliteas3.



Reply
Reply