CODE BASICS

ActionScript is a programming language in the Flash authoring environment
based on the JavaScript programming language. You use ActionScript to create RIAs or "rich internet applications". Basically this means that you can make your content interactive.

You can use ActionScripting to create a complex set of rules or an "algorithm" for how your project, application, website or kiosk will behave based on what the user does.

KEEP ORGANIZED

Keep your file consistently organized, so you and others you are working with can easily find things. Consistent and descriptive naming conventions, organizing the library with a hierarchy of folders and organizing your layers helps. Put your action scripts all on the same layer at the top. Put LABELS, ACTIONS, then SOUND and the content layers below.

Naming variables, functions and instance names

When naming variables and instance names, be short, descriptive and don't use any weirdo characters! %@^!&**#&!!! Use lowercase as well. Never start with a cappy, pappy. If you use two words, you can separate with an underscore or by "camel backing".

Examples:
game_over (good!)
gameOver (good!)
Game Over (bad!)

You cannot use words that are already a part of the action script syntax.

COMMENTS

Commenting code is important for two reasons. 1) You should always explain or "comment" your code so you or some other programmer can understand what you have done. 2) Comments can be used to disable code, which is useful in programming and debugging.

// This is a one line comment or one line delimiter.
//The slashes tell Flash to ignore this line.

// Comments are important as notes explaining what we have done with the code.
// You can comment out lines of code to disable them as you are working.
// This can be helpful for programming and debugging code (finding errors).

/* This is a multi-line comment or multiline delimiter
so you write notes about your code over
multiple lines. Cool huh?
*/

Here is an example for a button rollover:

myButton1.addEventListener(MouseEvent.ROLL_OVER, rolly);

function rolly(event:MouseEvent):void {
trace("hello!");
}


NOTICE (in the code above)
Dot syntax is how you put objects.properties.methods() together.

Semicolons terminate most lines of code, like a sentence period.

Curly braces group blocks of code that are executed together, as in a function.

Commas separate parameters in a method, like gotoAndPlay (20, "Scene 1");

Colons identify the type of object or in the case of a function, nothing is returned.

ARGUMENTS
A means of offering extra information or data to Flash when we execute a command.  This is done in parenthesis. 

gotoAndPlay(10); // the number 10 is the argument in this method that tells Flash to send the playhead to frame 10.
gotoAndPlay(“start”, “scene 10”) // multiple arguments in parenthesis are separated by commas. 
// This goes to frame label “start” in the scene named “scene 10”

OPERATORS
Operators are used to combine or otherwise manipulate phrases of code.  Some operators combine code in some way, others assign values, compare values, facilitate logistical decisions, determine datatypes (typeof), create new objects, etc.

Operators like + and – perform basic math.

var myVar = Math.random(100); // creates a random number and assigns to myVar

< > , ==  (equal to) or != (not equal to) make comparisons.

A single = assigns one thing to another.

EXPRESSIONS
Expressions are any phrase of code that yields a SINGLE datum when the program runs is called an expression.  For example, the number 1968 and the string “The year I was born.”  These are LITERAL expressions or LITERALS for short.  Variables are also expressions.

var message:String = ”hello world!”; // a variable expression
message+”  It’s great to be alive!” // variable and a literal expression

CREATING FRAME LABELS

One way to address where your Flash piece puts the playhead in your project is by using frame labels. You can also use frame labels to make a note or mark a spot in the working file.

Create a separate layer for LABELS above your ACTIONS layer. Click to select or create a blank keyframe and select it. In the property inspector, you will see the properties for keyframes appear. Under LABELS type the name for the label in the name area. Keep it simple and descriptive and use lower case. Use underscores or camel backing for spaces. For example, "start" or "end_game"

A little red flag-like graphic appears on the keyframe and the label name you typed (if there are enough frames).

CONTROLLING THE PLAYHEAD

These tell the playhead how to behave. The default is that the play head plays and all timelines loop unless you script otherwise. You specify the frame number or a label "name" in quotes between the parenthesis-

gotoAndPlay(20);
gotoAndStop("grumpy");
nextFrame();
nextScene();
play();
prevFrame();
prevScene();
stop();

gotoAndPlay(20); // Goes to frame 20 and plays.
gotoAndStop("explosion"); // Goes to the frame labeled "explosion" and stops