In addition, ActionScript (AS) is similar to other programming languages in syntax (or structure), so after learning AS code, you'll be able to slightly recognize other programming languages.
Flash Interface
One of the greatest tips I can provide is also one of the easiest, though it is often overlooked. If at anytime you don’t know what a reserved keyword (usually highlighted in blue) in the ActionScript palette means, highlight the word and hit the help button. This will bring up the extensive and well-documented ActionScript reference that provides in-depth descriptions and code examples.
If you wish to explore the structure of the language, you can always hit the plus button in the upper left of the palette. This will show the hierarchy of ActionScript code.
Strict Data Typing and Creating Code Shortcuts
Strict Data Typing is a way to ensure that a variable remains the same type of variable from when it has been created to when it is assigned a new variable. This creates more readable code and as an added benefit generates code hints (You can also create code hints through naming conventions, Example: naming MovieClip instances with “_mc” appended will create hints in the ActionScript).
In the past, you would create a variable in Flash by simply declaring:
// create a number variable
var myNumber = 10;
or
// create a number variable
myNumber = 10;
With ActionScript 2.0’s strict data typing, a variable would be declared as
// create a number variable
var myNumber:Number = 10;
As a result, assigning the variable anything other than a number at a later time would generate a compiler error.
// assign a String type to myNumber
myNumber = “This is a String”; // this would run an error.
Variable, Function, and Object Naming Conventions
Avoid spaces or special characters. These usually signify the beginning or end of a statement or part of an expression. Variable names can be comprised of letters, numbers, or underscores.
NOTE: the $ sign can be used to begin a variable (similar to PHP variable naming), but should be probably be avoided.
Names should start with a letter. This prevents Flash from interpreting files as an integer, etc.
Use unique names for variables, objects, and functions.
This will help Flash avoid any confusion between different elements of your movie.
Use an easy-to-understand system for naming. Developing a naming pattern will help a developer to identify different element types in a Flash movie. I suggest “Hungarian Notation”, a common naming convention in programming in which variables are identified by first word lowercase and subsequent words uppercase.
EXAMPLE: myVariable, myArray, firstName, etc.
Use multi-word descriptive names. While shorter names equal less code (generally meaning more efficient code), describing the variable or function with multiple words (variable name “leftSide” as opposed to “a” or “b”) will help you remember an element’s purpose in the long run.
NOTE: Flash SWF files should follow this naming convention - but should be all lowercase (EXAMPLE: myflashmovie.swf). This can avoid confusion if an SWF is hosted on a UNIX server. Also, avoid uppercasing the first word, as this usually represents a new class or object.
Avoid reserved words.
Words like “play”, “stop”, “_y”, “date”, etc. have reserved meanings in Flash and will generally throw off your code. A good way to avoid this is to look for colored keywords in the ActionScript window and use multi-word names.
Consolidate variable locations.
Will your variables be in the _root, _global, inherited, or local? Define a hierarchy of where to contain variables.
Consider defining variables with a var declaration.
While not required in Flash, this is a common convention in other programming languages and is used when defining temporary variables in functions. This definition format is required when doing strict data typing in ActionScript 2.0.
EXAMPLE: var myVarName;
Consider Strict Data Typing in ActionScript 2.0.
A new feature of Flash 2004 with ActionScript 2.0 is explicit declaration of a variable type. This prevents the wrong type of data being placed into a variable by assigning a variable’s data type.
EXAMPLE:
loose data typing (ActionScript 1.0): myVar = 7;
strict data typing (ActionScript 2.0+):myVar:Number=7;
With loose data typing, we can assign myVar to be a string, boolean, etc. later in the movie. With strict data typing, though, we cannot change the value to be anything except a number.
NOTE: Strict data typing cannot be used in _global variables.
Delete unused variables.
If your application no longer has use for a variable, delete it. This eliminates the variable information from your movie and clears the remote computer’s cache - it can really add up in performance.
EXAMPLE: delete myVarName;
NOTE: The delete command should be called only after your application has been tested to work.