3 May 2011
This article is intended for users who are familiar with Flash. Basic understanding of symbols, the Stage, timelines, keyframes, the Properties panel, testing movies, and motion tweens is required. Knowledge of ActionScript is helpful, but not required.
Beginning
Adobe Flash Professional CS5 has a feature called the Code Snippets panel that contains ActionScript 3 snippets that are commonly used in projects such as banner advertisements and games. If you've been searching for an easy way to start learning ActionScript 3 and want to add simple interactivity to your projects, then you should learn about this panel. Code snippets are useful not only individually, but can be mixed and matched to rapidly create complex interactive projects. Advanced programmers can use this panel to create and store custom snippets for later use.
Each snippet comes with instructions that explain how to use it and what code in the snippet needs to be modified. Use snippets to add simple interactivity to your projects quickly, without having to know much about ActionScript. Code snippets can also help you learn how ActionScript 3 code is constructed.
During this tutorial, you will learn how to do the following:
You will also learn various tips to get the most out of the Code Snippets panel.
The following snippets are featured in this tutorial:
Before you start the tutorial, it will help to know a little more about the Code Snippets panel and what it can do. The default code snippets are written in ActionScript 3. However, you can create your own ActionScript 2 snippets (or ActionScript 3 snippets) and save them in the panel if you want to. Code snippets can be applied to any shape, bitmap, video, text, or symbol. When you apply a code snippet, Flash automatically converts a shape to a movie clip symbol and gives it an automatically generated instance name, when one is not already specified.
After creating a new ActionScript 3 FLA file, there are two ways to get to the panel:
In addition to the snippets themselves, the panel features three buttons (see Figure 1):
Follow these steps to apply your first snippet:
This method is new in Flash Professional CS5.5. Follow these steps to apply a snippet through the HUD:
Take a closer look at the inserted instructions and snippet (see Figure 4). The shape you drew on the Stage is converted to a movie clip symbol called Symbol 1. The instance name movieClip_1 is automatically generated for you. The function name fl_ClickToGoToWebPage is also automatically generated. When you use snippets multiple times, the number used in the generated instance and function name increases; for example, movieClip_2, movieClip_3, fl_ClickToGoToWebPage_2, fl_ClickToGoToWebPage_3, and so on. You do not have to do anything special to create a clickable movie clip symbol that will take you to a specific URL. The only thing you need to modify to fit your needs is the URL in the URLRequest() call. Replace with the URL of your choice.
Now that you have learned the basics of the Code Snippets panel, you are ready to dive into the sample projects.
In this section, you are going to learn how to add and modify snippets by creating a banner ad that incorporates two snippets: Click To Go To Web Page and Click To Go To Frame And Play. Also, you'll learn how to replace the default mouse cursor with your own art work by using the Custom Mouse Cursor snippet.
Below is the completed version of the project you are going to make. Click on Play to try it. Shoot the UFO graphic with the mouse cursor. A separate browser window should launch and load www.adobe.com:
First you want to insert code that pauses at a specific frame:
/* Stop at This Frame
The Flash timeline will stop/pause at the frame where you insert
this code.
Can also be used to stop/pause the timeline of movieclips.
*/
stop();
Notice that the Actions layer is created in the Timeline, on top of all other layers. You have now inserted your first ActionScript code into Frame 1 of your movie.
button_1) for the >>PLAY button symbol. If you want to confirm this, select the button and look at the Properties panel. The Actions panel opens and the following instructions and code are inserted underneath the stop(); code you inserted in the step 5:/* Click to Go to Frame and Play
Clicking on the specified symbol instance moves the playhead to
the specified frame in the timeline and continues playback from that frame.
Can be used on the main timeline or on movie clip timelines.
Instructions:
1. Replace the number 5 in the code below with the frame number you
would like the playhead to move to when the symbol instance is clicked.
*/
button_1.addEventListener(MouseEvent.CLICK,
fl_ClickToGoToAndPlayFromFrame);
function fl_ClickToGoToAndPlayFromFrame(event:MouseEvent):void
{
gotoAndPlay(5);
}
gotoAndPlay(5); to gotoAndPlay(2);.You have just added user control to your project!
Next, make the flying UFO movie clip symbol clickable and launch the web browser with the URL adobe.com when it is clicked:
movieClip_2./* Click to Go to Web Page
Clicking on the specified symbol instance loads the URL in a new
browser window.
Instructions:
1. Replace with the desired URL address.
Keep the quotation marks ("").
*/
movieClip_2.addEventListener(MouseEvent.CLICK,
fl_ClickToGoToWebPage);
function fl_ClickToGoToWebPage(event:MouseEvent):void
{
navigateToURL(new
URLRequest(""), "_blank");
}
The final step in the first example is to add a custom mouse cursor:
movieClip_3 this time. The Actions panel opens and the following instructions and code are inserted in Frame 2 of the Actions layer:/* Custom Mouse Cursor
Replaces the default mouse cursor with the specified symbol
instance.
*/
stage.addChild(movieClip_3);
movieClip_3.mouseEnabled = false;
movieClip_3.addEventListener(Event.ENTER_FRAME,
fl_CustomMouseCursor);
function fl_CustomMouseCursor(event:Event)
{
movieClip_3.x = stage.mouseX;
movieClip_3.y = stage.mouseY;
}
Mouse.hide();
//To restore the default mouse pointer, uncomment the following lines:
//movieClip_3.removeEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor);
//stage.removeChild(movieClip_3);
//Mouse.show();
At the end of the snippet there are instructions for restoring the default mouse pointer. Since you want the default mouse pointer to be shown after the movie loops and goes back to Frame 1, you will need to place this restoration code at the end of the movie.
// characters from the start of each line of code.In this section, you are going to learn how to combine multiple snippets by completing an interactive project without using any timeline animation. You will learn how to use the Move With Keyboard Arrows and Play A Movie Clip snippets together. In addition, you'll also see how to use just part of a snippet.
Below is the completed version of the project you are going to make:
Follow these steps to enable the user to move the UFO movie clip symbol by pressing the arrow keys:
/* Move with Keyboard Arrows
Allows the specified symbol instance to be moved with the
keyboard arrows.
Instructions:
1. To increase or decrease the amount of movement, replace the
number 5 below with the number of pixels you want the symbol instance to move
with each key press.
Note the number 5 appears four times in the code below.
*/
stage.addEventListener(KeyboardEvent.KEY_DOWN,
fl_PressKeyToMove);
function fl_PressKeyToMove(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
UFO_MC.y -= 5;
break;
}
case Keyboard.DOWN:
{
UFO_MC.y += 5;
break;
}
case Keyboard.LEFT:
{
UFO_MC.x -= 5;
break;
}
case Keyboard.RIGHT:
{
UFO_MC.x += 5;
break;
}
}
}
In this sample FLA file, the instance name for the UFO movie clip is already specified as UFO_MC. Therefore, the instance name is not automatically generated when the snippet is applied. To confirm the instance name, you can select the UFO movie clip symbol and look at the name in the Properties panel.
Keyboard.LEFT case statement. Since the Left Arrow key is not used, delete the following lines of code:case Keyboard.LEFT:
{
UFO_MC.x -= 5;
break;
}
Next, change the event that is attached to the Right Arrow key. Instead of moving the UFO to the right, pressing the Right Arrow key will shoot the light beam:
/* Play a Movie Clip
Plays the specified movie clip on stage.
Instructions:
1. Use this code for movie clips that are currently stopped.
*/
UFO_MC.play();
You need to modify the inserted code UFO_MC.play(); because what you want to play is not the UFO movie clip. Instead, you want the beam-Parent movie clip inside of the UFO movie clip to play when the Right Arrow key is pressed. The beam-Parent movie clip is nested inside of the UFO movie clip so that it moves as the UFO moves.
beam.Note: You can double-click the beam-Parent movie clip to see the detail of the beam animation, but that is not necessary for this tutorial.
Now you know the instance name of the movie clip you want to play when the user presses the Right Arrow key. The correct code is UFO_MC.beam.play();. Because beam is nested inside of UFO_MC, you use dot syntax to reference the beam movie clip.
case Keyboard.RIGHT statement, replace UFO_MC.x += 5; with UFO_MC.beam.play();:case Keyboard.RIGHT:
{
UFO_MC.beam.play();
break;
}
/* Move with Keyboard Arrows
Allows the specified symbol instance to be moved with the
keyboard arrows.
Instructions:
1. To increase or decrease the amount of movement, replace the
number 5 below with the number of pixels you want the symbol instance to move
with each key press.
Note the number 5 appears four times in the code below.
*/
stage.addEventListener(KeyboardEvent.KEY_DOWN,
fl_PressKeyToMove);
function fl_PressKeyToMove(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
UFO_MC.y -= 5;
break;
}
case Keyboard.DOWN:
{
UFO_MC.y += 5;
break;
}
case Keyboard.RIGHT:
{
UFO_MC.beam.play();
break;
}
}
}
The Up Arrow key moves the UFO up five pixels. The Down Arrow key moves the UFO down five pixels. The Right Arrow key plays back the beam animation.
There is a lot more you can do with this sample file—for example, you may want to add buttons that show and hide the instructions. Open Sample2_Complete.fla to see how the rest of the code is implemented.
Now that you have experience using ActionScript 3 and the Code Snippets panel, I hope you'll agree that ActionScript is not scary when you can look at code as a well-designed block.
Once you get used to using snippets, you may want to do the following:
My colleagues and I have set up a blog for the Code Snippets feature. In the blog, we will continue introducing how to use each snippet and discuss where in your project they can be most useful. We will also cover more advanced topics such as customizing the Code Snippets panel.
Adobe Platform Evangelist Lee Brimelow has a popular blog that covers various topics on Flash and Flex, including ActionScript. The Code Snippets panel in Flash Professional CS5 is inspired by his version of the snippets panel.
I encourage you to use the Code Snippets panel to insert, combine, and modify snippets and create more advanced interactive content.
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.
| 04/23/2012 | Auto-Save and Auto-Recovery |
|---|---|
| 04/23/2012 | Open hyperlinks in new window/tab/pop-up ? |
| 04/21/2012 | PNG transparencies glitched |
| 04/01/2010 | Workaround for JSFL shape selection bug? |
| 02/13/2012 | Randomize an array |
|---|---|
| 02/11/2012 | How to create a Facebook fan page with Flash |
| 02/08/2012 | Digital Clock |
| 01/18/2012 | Recording webcam video & audio in a flv file on local drive |