• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Chapter 4. Basic Programming in Flash > Terminology, Special Characters, and Fo...

Terminology, Special Characters, and Formatting

The terms and rules in programming are very strict. This can actually be helpful. Unlike in English, where the meaning of a sentence can be ambiguous or vague, in programming there are absolutes. After you fully understand what a “property” is, for example, you never need to wonder what kind of property—as you would with the word property in English, which has multiple meanings. Not only do terms in programming have absolute meanings, they’re usually closely related to the word’s meaning in English. Be careful, however; sometimes your first impression of a term could carry a special meaning to you that is unrelated to the true meaning. Luckily, most terms are very easy to learn.


Everything that happens in Flash is the result of an event that causes a script to execute. Even for such a simple example as the user clicking a button that causes the movie to skip ahead, the instructions (to skip ahead) resulted from the click “mouse event.”

There are three types of events in Flash: keyframes, mouse events, and clip events. Notice that these correlate to the three places you can place scripts (keyframes, button instances, and clip instances). If you place a script in a keyframe, the script will execute as soon as Flash reaches the keyframe. In the case of buttons, you must always specify the mouse event to which you want to respond. Similarly, scripts on clip instances need to include a clip event. Figure 4.1 shows the available mouse events and clip events.

Figure 4.1. The mouse events (for buttons) on the top and the clip events (for clips) on the bottom include all the events available in Flash.

I like to think of an event as the wrapper of the scripts that it contains. Consider that you can’t just have a stop() Action attached to a button; you must wrap it within a mouse event. I suppose that it makes sense to think the stop() action follows the mouse event. But because you can have several actions follow one event (maybe the button causes both stop() and stopAllSounds() to execute), you need some indication of where the results of an event end. In Flash, the event wrapper starts with an opening curly brace ({) and ends with a closing curly brace (})—sort of like a “script sandwich” with two curly braces for bread and scripts in the middle. Even if that corny analogy doesn’t help, you should see that scripts are wrapped inside events. (See Figure 4.2.)

Figure 4.2. Everything between the two curly braces will be performed when the event occurs.

Results of Events

After an event happens, a script is executed. The instructions that Flash follows can range from simple to complex. Let’s start by thinking about the results of events in very general terms. First, we’ll look at the five types of tasks that happen as the result of an event:

  1. A script can do something that is invisible or unimportant to the user (such as add to a counter that is tracking how many times a button is clicked). Think of this type as laying the groundwork or defining what can happen later.

  2. The script can do something visual (such as change the rotation of a Movie Clip).

  3. The script can trigger another script to execute—effectively behaving like a homemade event. Perhaps the script attached to one button causes another script to rotate a Movie Clip. In this way, several buttons can trigger the same “rotate the clip” script and your code can be modularized.

  4. Scripts can “ask” other scripts for information (the part that’s said to be “returned”) and use the result to do one of the other tasks here. For example, one script could ascertain the current exchange rate from another script and use that information to translate a price from yen to dollars.

  5. Finally, the fifth result of an event is nothing. That might seem odd, but events happen all the time and nothing happens. For example, a linear movie with no buttons will have no response to the mouseDown event (despite the fact that it happened). It’s kind of like a tree falling in a deserted forest; that’s an event despite whether anyone reacts to it.


We can’t go much further without defining a few terms.


Syntax is the way any sentence is organized. In programming, a “sentence of code” is called a statement. The syntax, or the form of your statements, must adhere to strict syntax rules. One simple rule (that works in English, too) is that any parenthetical statement that you start with an opening parenthesis must be followed by a closing parenthesis. If your statement’s syntax is incorrect, your code will perform unexpectedly (at best) or not at all (at worst).

There are many ways that Flash helps you follow perfect syntax. While you are working in Normal Mode, the Actions panel will all but ensure that your syntax is perfect. Actually, as soon as your statement contains an error, it is highlighted in bright red. An explanatory message appears to guide you along, and often a code hint appears to help you with the proper syntax. Figure 4.3 shows a statement that isn’t complete. Remember that even after your syntax is perfected, it doesn’t mean your Flash movie will play the way you want it to. The script could still contain logical errors. The first step is to fix the syntax, and then you can ensure that it performs correctly.

Figure 4.3. When you edit your script in Normal Mode, errors are highlighted while you type.

Another way that Flash helps you produce perfect syntax is by color-coding certain terms. The Syntax Coloring option in the ActionScript Editor’s Preferences dialog box (accessed via the Actions panel’s options menu and shown in Figure 4.4) is a great way to learn to type (and spell) terms correctly. Keywords and identifiers (which, for now, you should simply consider as built-in terms of ActionScript) are colored dark blue. Comments (which are ignored by Flash and enable you to write notes to yourself or others reading your code) are colored gray by default. Finally, anything you type between quotation marks is blue. This is particularly useful because you must remember to end any quotations you start (just like parenthetical statements). If you start a quotation, everything that follows will turn gray until you type another quotation mark. You’ll find out more about keywords, properties, comments, and strings later in this chapter.

Figure 4.4. The ActionScript Editor’s Preferences dialog box enables you to change syntax coloring and fonts, in addition to other settings.

I wanted to mention the Syntax Coloring feature because it’s a great way to learn. For example, when you look through someone else’s finished code, it’s easy to separate the elements that are built-in to Flash (they’ll be blue) from the code that the programmer created (left in black). Possibly the most frustrating syntax rule in Flash is that all keywords are case sensitive. For example, goToAndPlay(2) won’t turn blue (only gotoAndPlay(2) will). Throughout this book, you’ll see many places where syntax is important. Syntax Coloring is just one little helper available to you.

Because Syntax Coloring is a preference, you can change any color to meet your needs. (Only the settings are saved with Flash MX, not the file). There’s also a new Auto Format feature that helps your code look neat. The Auto Format Options shown in Figure 4.5 are accessible from the Actions panel’s options menu. While we’re on the subject of “built-in features that help you learn,” be sure to study the way code hints work. There’s an explanation and table in Chapter 2, “What’s New in Flash MX.” All these helpers are by no means “cheating” because they help you to become a better programmer.

Figure 4.5. Auto Format Options specify how Flash MX cleans up your code as you type.

Objects, Instances, Instance Names, and Properties

In Flash, there are many types of objects, but the easiest kind to understand are Movie Clip instances. Every Movie Clip you drag from the Library onto the Stage becomes a separate instance. Properties are attributes of objects. In real life, you could compare properties of objects to personal attributes and characteristics of people. People have an eye color property, a height property, a weight property, and so on. If you have two instances on the Stage, they’re both based on the original master symbol but each is most likely positioned differently; that is, they’ll have different _x and _y properties. Not only can you change properties of individual clip instances while editing, but through scripting you can change each instance’s properties at runtime. This is easiest if the clip instance has been given an instance name (via the Properties panel). In pseudo-code, you’d say something like this: Set the _x property of the instance named “box_1.” Just remember that the instance name is what matters (not the master name in the Library).


By the way, Flash MX enables you to give instance names to several other types of objects, including buttons, text, and video. You’ll see this first in Chapter 10, “Keyboard Access and Modifying Onscreen Text,” but realize they have a few limits. For example, text must be set to Dynamic or Input. For now, just remember that a Movie Clip is no longer the only object with an instance name.

After you fully understand how to access clip instance properties, it will be easy to understand other objects. In Chapter 7, “The Movie Clip Object,” you’ll learn how to access and change instance properties. You will use the same techniques in Chapter 9, “Manipulating Strings,” Chapter 10, “Keyboard Access and Modifying Onscreen Text,” Chapter 11, “Arrays,” and Chapter 12, “Objects,” to control the sound object, string object, and color object (among others). Finally, in Chapter 13, “Homemade Objects,” you’ll create your own objects. The reason the Movie Clip is so much easier to understand is that it’s an object that you can see and touch. All the other objects are just as real, but you may never grab one with your mouse or see it onscreen. It’s sort of like how you can have a picture in your mind. Think of a physical printed photograph as the Movie Clip object— you can fold it, frame it, or hang it on the wall. Now, consider a picture in your mind as any of the other objects. Even though there’s no physical evidence, you can still manipulate the picture in your mind—picture it being folded, framed, and so on.

Statements, Expressions, and Operators

The entire block of text in the Actions panel is a script, where each line is a statement. As mentioned earlier, statements are “sentences of code.” It’s not so critical if you interchange the terms script and statement. But if a statement of code is like an entire sentence in speech, an expression is like a phrase. In speech, you might use the phrase as slow as molasses. First of all, you’d create an entire sentence (a statement), such as His computer is as slow as molasses. You can see that the expression (or phrase) is only part of the statement.

Expressions in Flash are evaluated. For example, an entire statement might read: halfprice=price/2. The part on the right of the equals sign is an expression and it will be evaluated (that is, a result will be placed in that spot). If it helps, think of evaluated expressions as having “math” done on them. Now, imagine that “as slow as molasses” is an actual speed (say 1 mile-per-hour). If you evaluated the expression, you’d be left with the result “1 MPH.” The original sentence would become His computer is 1 MPH. This analogy is beginning to fall apart, but it’s a good way to learn about statements and expressions (as well as the standard terms evaluate and result).

Finally, operators perform specific (usually math) operations. They always need at least one number, called an operand, on which to work. For instance, “+” is an operator that performs addition. The “+” addition operator requires two operands to work. Get it? There’s the operator (doing the work) and the operands (getting work done on them). The thing to remember is that operators are used within expressions. Therefore, they’re evaluated and their results appear in their place. For example, saying whatever=2+2 is the same as saying whatever=4. The expression on the right is evaluated and turns into 4. This might seem simple. Just think of expressions morphing into their results.

Operators are pretty easy to understand when you consider familiar math operators—such as multiplication (*), division (/), and subtraction (-). Others aren’t so easy to figure out and might even seem arbitrary. For example, one deceptively useful operator uses the percent sign (%), but not to calculate a percent. It’s called the modulo operator and results in the remainder of two numbers. 10%2 is evaluated into 0. Ten divided by 2 has 0 for a remainder. This can be useful to determine whether a number is odd or even (or evenly divisible by any other number, for that matter); that is, if the number % 2 is 0, it’s an even number because it’s evenly divisible by 2. Anyway, I don’t recommend memorizing all the operators. We’ll use many of them in the Workshop section, especially when we make sliders and the currency exchange calculator. However, after you understand the form—that operators create an expression that’s evaluated and they always require at least one operand—you can learn them gradually.

Actions and Functions

In Flash, “actions” have many meanings. I think it’s best to think of actions as Flash-unique commands that execute specific tasks. For example, the stop() action is a command that causes the playback head to stop. The gotoAndPlay ("framelabel") action is another command that jumps to a frame number or label and plays. These actions do things specific to Flash. However, the Actions panel lists many “actions” that are, technically, statements (or elements of statements). Figure 4.6 shows some actions that are really statements. For example, if is a piece of code that creates a statement. You have to specify additional details (such as, “If what?”), but if was around way before Flash—and in my book, it’s a statement. For the sake of understanding, I’ll try to avoid the term “actions” completely.

Figure 4.6. All the “actions” in Flash are actually a mix of statements and Flash-unique commands.

Keep in mind that although the term “actions” is made up, actions behave in a consistent manner. All the actions in Flash are one of two types. They’re either specific Flash commands (such as stop, duplicateMovieClip, and play), or they are statements (such as if or break). (Actually, you’ll learn in the next chapter that a more common name for many actions is method.) Many actions (of either type) need additional parameters provided—for instance, gotoAndPlay() requires a frame label or number. Another consistent attribute of all actions is that they’re never evaluated; they do things. Actions have consequences. (To avoid confusion, don’t call them “results.”) The script you create using actions doesn’t morph into a result the way that expressions do. Actions simply “do things.” Either they help construct a statement or they do a Flash-unique task.

Although Flash’s built-in functions are easy to confuse with actions, they are different in a profound way. A function always returns a value. That is, a built-in function’s job is to calculate an answer. Similar to how an expression is evaluated, a function “returns” a value, which, in turn, is placed where the function was called. For example, the function getTimer() returns the total number of milliseconds elapsed since the movie started. It’s almost like a “what time is it?” function. But consider if you asked someone, “What time is it?” That person might look at her watch, but unless she “returns” the answer, you might never know what time it is. Similarly, if you just wrote the script getTimer(), it would be meaningless. The getTimer() would turn into a number—maybe 10200, but so what? If, however, you wrote elapsedTime=getTimer()/1000 + " seconds", the getTimer() part would turn into 10200 and the expression on the right side of the equation would evaluate as 10.2 seconds.

All the built-in functions behave this way. Consider these real-life activities as actions: drive a car, stop the car, fill the car with gas. Now consider these as functions: getting today’s price of gas, determining how many liters fit in a 20-gallon gas tank, determining how many yen are equal to 10 dollars. The functions always result in an answer (which can be used within a bigger calculation). Notice, too, that some functions just provide an answer (such as “getTimer” or “getting today’s price of gas”). However, other functions (such as “converting gallons to liters”) require additional data to be provided (that is, “How many gallons?”). In these cases, you need to provide a parameter.

In Chapter 8, “Functions,” you’ll learn all about functions—both the built-in functions and homemade functions. For now, just understand that actions are either Flash-unique commands (also called methods) or statements, and that built-in functions always return values. The big difference is that an action can be used by itself, whereas a function is always used within a larger script.

Special Characters and Formatting

As part of the many syntax rules in Flash, certain characters have special meanings. Additionally, the layout or format of your scripts must follow some rules. Parentheses are a perfect example of this point. Any parenthetical statement must start with an opening parenthesis and must always end with a closing parenthesis. The reason this is important is that you can have nested parentheses like this: ((6+2)*3)/2, which results in 12. That’s different than 6+((2*3)/2), which results in 9. The innermost expressions are evaluated first, followed by expressions in outer parentheses. The point is that anything you start, you have to finish. For example, ((6+2)*3/2 will cause an error because there’s an extra open parenthesis.

There are identical rules for quotation marks ("), brackets ([ and ]), and curly braces ({ and }). Quotation marks are a little different because apostrophes will work the same as quotation marks. This way you can nest quotations in quotations. You can’t mix and match, however. For example, all of these are legitimate:

"This is 'cool' " 
'Flash version "MX" is More eXemplary than "5"' 
"Phillip’s dog is named Max"

But these won’t work:

"What you "start" you have to "finish"" 
"Mixing will not work’

It’s interesting because in the case of "Phillip's dog… ", you might think that the apostrophe would cause problems. It works fine, however. Also, nested quotation marks are different than nested parentheses. You can have two open parentheses in a row, and as long as you eventually close what you open, they will work fine. However, two quotation marks in a row will cause Flash to think that the second one is closing what the first one opened. So, if you have to nest quotations in quotations, you must use quotations for one and apostrophes for the other. (By the way, the curly, or “smart,” quotation marks, “ and ”,won’t work in the Actions panel.)

Generally, spaces and return characters (at the end of a line) are ignored by Flash. If you start a parenthetical statement and close it five lines later, there’s no problem. Because you can’t simply create a new line to represent a new thought, every line of code can be terminated with a semicolon. For example, look at this piece of code:

if (age>18){

Even though we haven’t looked at the if statement yet, you can see that the open curly brace is followed (three lines later) by a closing curly brace. Also, there are two separate lines of code, each ending with a semicolon. Actually, the following is also totally legitimate:

if (age>18){voter=1;draftable=1;}

The first example was easier to read. In either case, the semicolon clearly ended individual statements. Occasionally you can be sloppy and, based on context, Flash will figure out what you intended. It’s best to deliberately use semicolons to end each line, however.

One of the best character sequences to learn is the double forward slash (//), which creates a comment. Comments are ignored by Flash, so you can write anything you want. You can use comments as notes to yourself (or anyone else who might be viewing your code). The phrase “commenting your code” means that you include all the information necessary to make sense of your code through comments. Also, comments are useful to cause Flash to temporarily ignore part of your code—effectively removing the code, but leaving it so that you can restore it easily.

Everything that follows // (on the same line) is ignored. You’ll see the commented text turn pink, which makes it very easy to recognize. The following code sample includes comments:

//This button makes the movie stop 
on (release) {
    stop(); //don't go any further 

Normally, comments automatically end at the end of a line. (Notice that there’s no “closing” comment mark in the code.) However, you can use /* to start a block of comments that will continue until a */ is reached.

/* Comments: 
    This is a comment. 
    This is another comment. 

We’ll encounter other special formatting and new terms throughout this book, but this should give you a good start. Now, with the terms and other technical matters out of the way, we can move on to variables and how to use them.

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint