Flash Tutorial: Button using Movie Clip symbol.
This entry was posted on Wednesday, March 12th, 2008 at 12:16 am and is filed under Flash, Flash Tutorial, Tutorials, Web Design.
You can follow any responses to this entry through the RSS 2.0 feed. Responses are currently closed, but you can trackback from your own site.
Flash Tutorial: Creating a button using a movieclip and ActionScript
Level: This is a beginner lesson, but I will assume that the reader is familiar with the basic use of the Flash software including stage, timeline, drawing tools, property window, etc..
Version: I will be using Flash CS3 and AS2 (athough tut is compatible with earlier versions of Flash) and instructions are for the Mac. Windows users would use the PC counterparts for specific instructions on Flash Menu/Shortcut instuctions.
Demo:
Description: In this tutorial, I will explain how to create a button as a movieclip symbol using ActionScript rather than using the button symbol.
Let’s get started!
Select “File > New” (command-”N”) and select Flash File (ActionScript 2.0) to open a new Flash document file. Save the file, and give it the appropriate name. I will call this “button_tut1.fla”. Feel free to change the document dimensions to whatever you like (“Modify > Document or “command-J”), but for the purpose of this tutorial, I will use 250×100 pixels.
The first thing we will need to do is draw a simple shape for our button. Select the rectangle tool, set your fill color to whatever you would like (I chose red), and a stroke of none. In the property window (Window > Properties > Properties or command-F3), set the corners to rounded at 9 (Fig. 1). Draw out a rectangle approximately 100×20 pixels. Tip: You can use the up or down arrows on your keyboard when dragging out your rectangle shape to increase or decrease the roundness of your corners.

You should have a rectangle similar to the Fig. 2.

Select the rectangle, and convert to a movie clip symbol (Modify > Convert to Symbol or F8). Remember, we are creating a movie clip button, so be sure to select the “movie clip” radial. Give your symbol an appropriate name, I called mine “mc_button”.
—- ads by google —-
Now, let’s add a little styling to our button. Double-click on our new symbol to bring us into the symbol editing mode. Select the rectangle, and Edit > Copy (command-C). In the timeline, name your layer “button solid” and create a new layer called “button highlight”. Lock the “button solid” layer, and move it to the bottom layer. On the “button highlight” layer, do a paste in place (Edit > Paste in Place or shift-command-V). With this rectangle selected, change the width to 92 and the height to 5 in the Info pallette (Window > Info or command-I (Fig. 3)), and select a linear gradient fill. In the color pallete, set your gradient scale to be 100% white on one end and 0% white on the other. With the rectangle still selected, select the gradient tool (located with your transform tool in the tools pallette), and rotate your linear gradient to be vertical (Tip: Hold shift when rotating to snap to 45º angles), with the 100% white on the top and the 0% white on the bottom. Also, shrink the gradient tool so that it is the height of your rectangle. Now, position this rectangle slightly below the top edge of your solid rectangle, and centered from side to side. You should have a shape that looks like Fig. 4.


Create a new layer, and call it “text”. Add a text block centered on your button shape and type “button”. You will now have something that looks like Fig. 5.

We are now done with the first state of our button, and now we can add the functionality that maves our movie clip behave like a button. For that, we will need to add some labels and then modify our button on the different frames.
Still working in your button symbol, add a new layer and call it “labels”. On your timeline, add frames to your timeline so that it extends to frame 45 for all layers. Insert a new keyframe (Insert > Timeline > Keyframe or F6) at frame 15 and 30 of the “labels” layer. click on frame 1, and in the Properties window, type “btn_off”. Repeat the process at Frame 15, but label it “btn_over”, label frame 30 “btn_down”. Add a new keyframe to the “button solid” layer at frame 15 and frame 30. You should now have a timeline that looks similar to Fig. 6.

At frame 15, select the “button solid” layer, and change the color of your shape, I’ve used a green for this example. Do the same thing at frame 30 and use a third color, I’ve used a blue. These color changes are simply for illustrative purposes. You can modify the buttons however you like, I just wanted to show a drastic change for when we add the ActionsScript that gives our movieclip a button functionaily.
We are now ready to start adding some ActionScript. Still within your button symbol, create a new layer, move it to the top of the timeline and label it “actions”. Add keyframes at 15 and 30. Click on frame 1 of your “actions” layer, and in the Actions panel (Windows > Actions or alt-F9) and add the following code:
stop();
This simply stops our moviclip from playing automatically (remember, we want the changes to take effect when the mouse rolls over the rectangle, like a button). Add the same ActionScript to the “actions” layer at frame 15 and 30. Our button symbol is now complete, so we need to return to the main timeline (Scene 1) where we will be adding the ActionScript that controls the button functionality.
Click on the button symbol, and give it an appropriate instance name in the Property Window. I’ve used “mc_button”. With the symbol still selected (but NOT in editing mode), add the following code to the Actions panel:
on (rollOver) {
_root.mc_button.gotoAndStop(“btn_over”);
}on (rollOut) {
_root.mc_button.gotoAndStop(“btn_off”);
}on (press) {
_root.mc_button.gotoAndStop(“btn_down”);
}on (release) {
_root.mc_button.gotoAndStop(“btn_off”);
getURL(“http://www.spitshine-design.com”);
}
UPDATE: I’ve been finding several readers having trouble with doing a straight copy/paste of this code from the Web. I believe the issue lies in the quotation marks (“). When copying from the Web, it sometimes uses “smartquotes”. If you are getting an error on test regarding the quote marks, first step is to be sure you are using regular quotes (type them in instead of copy/paste from the Web site). If you are still having problems, feel free to drop me a comment and I will try my best to help resolve your issues. – Mike
That’s it, our movie clip as a button is now complete. Go ahead and test your movie and check the button state change when you roll on, roll off, mouse down or mouse release.
Now I will try and explain the code.
on (rollOver) {
This is saying when the user rolls their mouse over the button, perform the actions within the curly brackets.
_root.mc_button.gotoAndStop(“btn_over”);
}
This is telling flash to go to the instance of “mc_button”, and go to the frame labeled “btn_over” and stop.
on (rollOut) {
_root.mc_button.gotoAndStop(“btn_off”);
}on (press) {
_root.mc_button.gotoAndStop(“btn_down”);
}on (release) {
_root.mc_button.gotoAndStop(“btn_off”);
getURL(“http://www.spitshine-design.com”);
}
This code is basically doing the same thing as above for the various mouse events (roll off, click and release).
getURL(“http://www.spitshine-design.com”);
This is the destination URL for when a user releases the mouse button while over the button. You can set this url to whatever you like.
While this tutorial seems pretty lengthy, it’s really because I wanted to be as detailed as possible so users can understand what is happening. Once you do this once or twice, I think you will find that creating movie clip buttons is fast and simple, and as you learn more about Flash, you will be able to create more animated buttons rather than just a simple color change. I plan to do another tutorial in the near future that illustrates some of the advanced effects you can apply to a button when you create it using the movie clips symbol as opposed to the button symbol.
I hope this Flash tutorial was helpful, and as always comments, questions or feedback are always welcome!
Source files: Flash Button Tutorial: Source File
Tags: design tutorial, flash design, flash development, Flash Tutorial
Flash Tutorial: Play movie clip with mouse click




