In this lesson, you’ll learn to do the following:
• Use the Code Snippet panel in Flash CS5.
• Navigate to a URL using a code snippet.
• Add ActionScript to the Timeline via the Actions panel.
• Add labels to frames on a timeline.
• Control playback with ActionScript you’ve added to the Timeline.
• Preview your Flash project as a SWF file in the testing environment.
• Change the content of a dynamic text field in ActionScript.
• Use an ActionScript variable to keep track of a changing number.
• Use a conditional statement to respond to the looping of an animation.
This lesson will take approximately 2 hours.
The Flash Timeline is an extremely useful tool for creating animations. It is also a great environment for setting up a website or simple application that requires navigation between different sections of content. This lesson introduces the techniques for adding code to the Flash Timeline to control playback; it also introduces a few essential ActionScript 3.0 programming concepts.
First, though, this chapter introduces you to a great new feature in Flash CS5 called code snippets. Code snippets are designed to help new programmers learn ActionScript 3.0 and to speed up the workflow for experienced ActionScript 3.0 developers.
To begin this lesson, from the Lessons > Lesson01 > Start folder, open the lesson01_start.fla file in Flash CS5. This file has layers, graphics, and animation. If you scrub (that is, click the playback head and drag to the left or right) through the Flash Timeline, you’ll see that the first 30 frames contain an animation and that Frame 50 contains the background for an interactive interface.
If you ran the movie at this point, it would just play through the whole Timeline from start to end, showing a brief glimpse of the interface before returning to the beginning. You will soon add ActionScript directly to this file to alter the playback of the Timeline, but first you will make use of the Code Snippets panel to add a link to a URL when a button is clicked.
You can create ActionScript on any keyframe in the main timeline of a Flash movie. You can also create it on any keyframe within a movie clip symbol. During playback of the compiled Flash project, the code on each frame will execute when that frame plays.
All timeline code in Flash is written in the Actions panel, accessible in Flash from the Window menu or by pressing F9 (Windows) or Option+F9 (Mac).
Later, you will see that you can also write ActionScript in external ActionScript files, but for now, you will create code exclusively in the Actions panel.
In addition to typing code directly in the Actions panel, you can add code to this panel using the new Code Snippets panel. Code snippets, as their name implies, are prebuilt chunks of ActionScript 3.0 code that can easily be added to your projects and modified to suit your purposes. Code snippets provide a wonderful way to begin working with ActionScript and can be very useful for increasing the range of tasks that you can accomplish.
The Code Snippets panel is found on the Window menu in Flash CS5. When you add a code snippet to your project, the code snippet is written in the Actions panel, saving you the trouble of typing it yourself. After the code snippet is placed in the Actions panel, it is fully editable, allowing you to customize and modify that code.
To see how code snippets work, you will add some code that will link to the Flash support page at www.Adobe.com when a button is clicked.
Begin by adding a button to the Stage:
buttons
layer.The button that you are working with is a component that ships with Flash CS5. In previous versions of Flash, component parameters, such as the label property of the button that you just modified, could be modified in a panel called the Component inspector. This panel has been deprecated in Flash CS5, and component parameters are now modified in the Property inspector instead. You will learn more about components in coming chapters and will see that component parameters can also be controlled using ActionScript.
Depending on the functionality needed, code snippets can be applied in a few different ways. For a code snippet that is intended to execute when the user interacts with a button, as is the case here, you apply the snippet by first selecting the button onstage and then applying the code snippet. You will do this now with help_btn
:
help_btn
instance onstage.actions
layer of the Timeline now has a lowercase a in it. This indicates that the ActionScript written by the code snippet has been stored in this frame. You should also see the following code appear in the Actions panel:If you are used to playing the Flash Timeline by pressing Enter (Windows) or Return (Mac) or choosing Control > Play, then you should switch to Control > Test Movie > in Flash Professional or Control+Enter (Windows) or Command+Return (Mac). When you preview in this manner, your files will play in the same way as they will for your end users, with all of the ActionScript executing. From this point on, the instruction to test your movie refers to this method of previewing Flash files.
help_btn
. The code should cause the Adobe home page to open in the computer’s default browser when the button is clicked.Because code snippets are pure ActionScript code, they can easily be modified to change their functionality. If you examine the code snippet that was written in the Actions panel, you will notice that the first section of code is gray and contains a description of the code that was written and some instructions for modifying it. All of the code snippets that ship with Flash CS5 come with comments like this that help you understand the ActionScript written by the code snippet. Comments will be discussed in more detail soon and are good elements to add to your own code as notes for yourself and others.
For now, follow the instructions in the code snippet to modify the URL in the ActionScript:
navigateToURL(new URLRequest("http://www.adobe.com"), "_blank");
http://www.adobe.com/support/flash
By the end of these lessons, you will know the ActionScript necessary to write all of this code from scratch, and that knowledge will open infinite creative possibilities. In the meantime, you can use the actions available in the Code Snippets panel to immediately start adding interactivity to your projects. Using this panel will also aid in your learning process by giving you insight into how to create working code and how to modify it to suit your needs.
You will work with the Code Snippets panel again in the next lesson, but now you will begin to write your own ActionScript code.
Before you start adding ActionScript, notice that among the layers in the Timeline is one called labels
. In this layer, Frame 2 has the label loop
and Frame 50 has the label home
. You can add labels to any keyframe on any timeline in Flash—labels can be very useful for identifying significant locations. ActionScript can reference labels to control navigation. If you haven’t worked with labels before, you may want to practice adding an additional label to the Timeline on Frame 30:
labels
layer of the Timeline, select Frame 30.There are many situations in which you may want to play a section of the Timeline repeatedly. For example, an animation might loop while waiting for additional content to load or while the user is deciding which section of a website to go to next.
For your first foray into writing your own ActionScript, you will loop the animation that plays from Frame 2 to Frame 30. At first, you will write ActionScript that loops this section indefinitely, and then you will add code that controls the number of times that this section repeats before jumping to the home
interface on Frame 50:
actions
layer, select Frame 30 and press F6 to add a new keyframe in that layer.actions
layer in Frame 30.gotoAndPlay("loop");
home
frame but instead plays the opening animation over and over. This behavior occurs because every time the playhead reaches Frame 30, the action you just wrote sends it back to Frame 2.In the next section, you will program your file to jump to the home
frame after a specific number of loops, but first you’ll add some code to keep track of the number of times that the animation has played. You will do this by storing the value for the number of loops in a variable.
The purpose of a variable is to store a reference to data. ActionScript 3.0 can store references to many different types of data in variables, and you will create many of these in the coming lessons. In this lesson, you will create a variable to store a numeric value to keep track of the number of times the animation in Frames 2 through 30 repeats.
To create a variable in ActionScript 3.0, you type var and then the name you want to give to your new variable:
actions
layer, select Frame 1 and then click in the Actions panel.var count:Number = 1;
In the next task, you will add some code that changes that value every time the animation loops.
To change the value of the count
variable on each loop, you add some code to change the value each time the animation finishes:
actions
layer, select the keyframe on Frame 30.count++;
The characters ++ are a shorthand way in ActionScript to increase a value by 1. The code you just wrote loosely translates to “take the current value of the count
variable and add 1 to it.” The result is that the second time the animation plays, count
will equal 2, the third time it plays, count
will equal 3, and so on.
At this point, you have a variable keeping track of the number of loops, but when the movie runs, you have no visual feedback telling you how many times the animation has looped. You will now add that feedback in a text field:
text
layer. Notice in the Properties panel that this text field has been given the instance name info_txt
. Again, it is essential that all objects onstage that will be controlled with ActionScript be given instance names.actions
layer, select Frame 2, and add a keyframe by pressing F6.info_txt.text = String(count);
A project that just plays its introduction over and over is not ideal, so let’s limit the number of times the introduction loops before jumping to the home
frame. For a web project, you might want to set up an introduction that plays over and over until the next section is fully downloaded. You will learn how to create this type of preloader in Lesson 6, “Creating Preloaders in ActionScript 3.0.” In this lesson, you will limit the number of times that the introduction plays by making the Timeline jump to the frame with the label home
(Frame 50) when the count
variable reaches a certain number.
You will accomplish this by adding a conditional statement to your code. A conditional statement in ActionScript checks to see if a condition is true. In this case, if the count
variable is more than 4, then code will execute that goes to the home
frame.
actions
layer.gotoAndPlay("loop");
Then above the line that says count++;
add the following code:
if(count>4) {
Now each time the animation finishes, the conditional statement checks to see if count
has become greater than 4. On the first four loops, when the condition was not true, the playhead goes back to Frame 2 and replays the animation. After the fifth time the animation plays, the condition is true, so the playhead goes to, and stops on, the home
frame.
home
frame.Now you will change the text on the home
frame. Since the user is no longer watching the animation loop on this frame, there is no reason anymore to display the count number. Instead, you will add a message to welcome the user to the home
frame.
actions
layer.actions
layer, add a keyframe to Frame 50 by pressing F6.info_txt.text = "Welcome to the home frame";
This code uses the same text field that you used before, but instead of using the count
variable to populate the text field, you use the literal words “Welcome to the home frame.” When you want to set the text
property of a text field to literal words, you enclose the characters you want to use in quotation marks.
home
frame.Although the application you just created is very simple, it introduced a number of essential ActionScript concepts. Storing and passing data with variables and responding to changing circumstances by using conditional statements are both critical elements in the creation of rich interactivity with ActionScript 3.0. You will be working with these techniques often in the lessons to come.
In the next lesson, you will learn how to respond to events in ActionScript 3.0 by using buttons to add some functionality to the file you created in this lesson.
To get comfortable with the techniques introduced in this chapter, you can try some of the following techniques:
• Change the number of times that the opening animation repeats by altering the conditional statement.
• Change the code on Frame 50 to display different text in the info_txt
field.
• Add a new text field to the Stage. Give it an instance name and try to write some ActionScript that will place text in that field.
• In a new file, explore some of the other ActionScript that can be created with the Code Snippets panel. Experiment with modifying this code. Remember that if you cause the code to stop functioning, you can always reset the code snippet.
1 What ActionScript code would you use to navigate to a specific frame of the Timeline?
gotoAndPlay(1);
gotoAndPlay("home");
2 What is the keyword that you use to create a new ActionScript variable?
3 What is the purpose of a conditional statement in ActionScript?
1 To navigate to a specific frame of the Timeline, you would use gotoAndPlay()
or gotoAndStop()
. The value between the parentheses describes the specific frame to which you want to navigate. For example:
2 A line of ActionScript that creates a variable begins with the keyword var
.
3 A conditional statement lets you check to see if a condition or conditions are true and, if so, execute blocks of code. If the condition is false, you can execute an alternative block of code.
3.144.252.140