Coding Concepts
There are many different parts of the ActionScript language, some of which you have explored and even written so far. There are particularly important parts of the language to learn the fundamentals of, regardless of whether you continue on to create business applications for the boardroom or fantastic graphic effects for a bleeding edge website. However, there are particular parts of the language that are well-suited to animation. You cover the basics of ActionScript syntax here, and in the following chapters you'll continue to piece together more pieces of the puzzle. Syntax forms the rules and guidelines for a particular way of writing your code.

Using data and declaring variables

Naming Variables
There are several considerations to make when you name a variable. Variables can contain only letters, numbers and underscores. The name can be anything you want, except that it cannot share a name with another variable. So, you cannot have two variables called myVar. Variables are case sensitive, so you could use myvar and myVar as two variable names, except that this is confusing and should be avoided. You should also try to avoid special characters.Variables cannot be a keyword; or share the name of an object, method, or property in ActionScript. You want to avoid any part of the ActionScript language, in fact. For example, if you use a keyword as a variable, the variable is treated like the keyword instead, which then breaks your code. For a list of words to avoid, see "Avoiding reserved words" in the Using ActionScript book of Flash documentation (Help > Help). Search "reserved words" in the Help panel to find this section.When you declare a new variable, you use the var keyword. You use the var keyword to declare (create) the variable.


The variable myLuck contains the Boolean value of false.Later on in the application, the value can be set to TRue if luck changes. Or, if you use the variable myLuck several times in your code, and later on you want all of those values to be true, all you have to do is change the line above and all the values are updated everywhere you might use it.For example, perhaps you want to have a series of movie clips that are quite transparent. You might set the transparency in your code, which is set by using a Number value:
var myLuck = false;
Understanding Objects" (later in this chapter).You have set 30 movie clips to myAlpha. Later on, you might change the design of your site and decide that much transparency detracts from the design. All you need to do is change the single myAlpha value from 50 (its initial value) to 20, and every single clip updates. Imagine if you set each clip to the number 50 instead of myAlpha. You would have a lot of updating to do in your code.
var myAlpha = 50;
Using dot notation
ActionScript uses dot notation, sometimes called dot syntax, to address (or target) objects in your SWF files. For example, you need to target a movie clip instance before you can apply an action to it. Dot notation helps you create a path to the instance you need to target.


How ActionScript manipulates instances" later in this chapter.This code targets the myClip_mc instance, which sits on the main Stage. Then the ActionScript targets the myClip2_mc instance, which is inside the myClip_mc instance. Finally, you add the action that you want to occur: The gotoAndPlay() method plays the myClip3_mc instance, which sits inside of myClip2_mc.Remember that each movie clip instance has its own Timeline, which you can not only target with code, but also place graphics, code, or other instances on. So you would use this code if you want to target myClip3_mc and have it go to frame 5 in the movie clip and play that frame onward. Notice that the clip that's manipulated comes right before the action. You'll notice this trend in upcoming sections.It might seem pretty confusing, and it certainly can be. You should try to keep your FLA documents as simple as you can, but there will be times that you encounter complex nested instances that you need to target by using code similar to the snippet just outlined.
myClip_mc.myClip2_mc.myClip3_mc.gotoAndPlay(5);

Adding Scripts Using the Add Button and Toolbox
You can use the Actions toolbox to help you add code to your FLA file if you're new to writing ActionScript or are unsure of ActionScript syntax. The Actions toolbox is located to the left of the Actions panel. The Actions toolbox contains all the actions you can use that are built into Flash. The toolbox contains many "books" that you can click and move throughout to find actions, methods, and properties. When you double-click a method in the Actions toolbox, for example, it adds to the Script pane wherever the cursor is located. You can also use the Add (+) button to add new scripts to your code (Figure 6.7). This useful feature lets you add pieces of code to the Script pane when you select an action from the menu. In some cases, after you add the action, the cursor in the Actions panel is specifically placed where you need to type, and a tooltip appears. The tooltip provides a guide on how you should go about finishing off the script.
Figure 6.7. Use the Add button in the Actions panel to help you add code to the FLA document.
[View full size image]

Commenting Code
When you add code to your Flash documents, the most important thing to remember is to add enough comments to your code. Comments can be any text that you want to write, such as what the code is doing, or any helpful hints you want to add. Comments help you remember what exactly the code is doing if you have to modify the code in the future. Comments also make it easier for other people to understand the code when they modify your FLA files.Place // in front of a line of code to add a comment, and then type anything you want (the comments) onto that line. Or, if you want to add a large comment, you can type /* at the beginning of the first comment line, and then add another */ at the end of the comment. The comment will be a different color than the rest of your code in the Actions panel (comments are gray by default).Also try to give your variables descriptive names, which can help with the readability of your ActionScript. If your variables describe what they contain, then other people can better understand what they're used for. Don't make your variable names too long (this helps your performance), but make sure they still describe that they're used for in the SWF file.