Writing Scripts with ActionScript > Using ActionScript's syntax
Using ActionScript's syntaxActionScript has rules of grammar and punctuation that determine which characters and words are used to create meaning and in which order they can be written. For example, in English, a period ends a sentence. In ActionScript, a semicolon ends a statement.
The following are general rules that apply to all ActionScript. Most ActionScript terms also have their own individual requirements; for the rules for a specific term, see the its entry in ActionScript dictionary: Overview.
In ActionScript, a dot (.) is used to indicate the properties or methods related to an object or movie clip. It is also used to identify the target path to a movie clip or variable. A dot syntax expression begins with the name of the object or movie clip followed by a dot, and ends with the property, method, or variable you want to specify.
For example, the _x
movie clip property indicates a movie clip's x axis position on the Stage. The expression ballMC._x
refers to the _x
property of the movie clip instance ballMC
.
As another example, submit
is a variable
set in the movie clip form
which is nested
inside the movie clip shoppingCart
. The expression shoppingCart
.form.submit = true
sets the submit
variable of the instance form
to true
.
Expressing a method of an object or movie clip follows the same pattern. For example, the play
method of the ballMC
instance moves the playhead in the Timeline of ballMC
, as in the following statement:
ballMC.play();
Dot syntax also uses two special aliases, _root
and _parent
. The
alias _root
refers to
the
main Timeline. You can use the _root
alias to create an absolute target path. For example, the following statement calls the function buildGameBoard
in the movie clip functions
on the main Timeline:
_root.functions.buildGameBoard();
You can use the alias _parent
to refer to a movie clip in which the current movie clip is nested. You can use _parent
to create a relative target path. For example, if the movie clip dog
is nested inside the movie clip animal
, the following statement on the instance dog
tells animal
to stop:
_parent.stop();
See Working with movie clips: Overview.
Slash syntax was used in Flash 3 and 4 to indicate the target path of a movie clip or variable. This syntax is still supported by the Flash 5 Player, but its use is not recommended. In slash syntax, slashes are used instead of dots to indicate the path to a movie clip or variable. To indicate a variable, you precede the variable with a colon as in the following:
myMovieClip/childMovieClip:myVariable
You can write the same target path in dot syntax, as in the following:
myMovieClip.childMovieClip.myVariable
Slash syntax was most commonly used with the tellTarget
action, whose use is also no longer recommended.
Note: The with
action is now preferred over tellTarget
because it is more compatible with dot syntax. For more information, see their individual entries in the ActionScript Dictionary.
ActionScript statements are grouped together into blocks with curly braces ({
})
, as in the following script:
on(release) { myDate = new Date(); currentMonth = myDate.getMonth(); }
See Using actions.
An ActionScript statement is terminated with a semicolon, but if you omit the terminating semicolon, Flash will still compile your script successfully. For example, the following statements are terminated with semicolons:
column = passedDate.getDay(); row = 0;
The same statements could be written without the terminating semicolons:
column = passedDate.getDay() row = 0
When you define a function, place any arguments inside parentheses:
function myFunction (
name, age, reader
){
... }
When you call a function, include any arguments passed to the function in parentheses, as shown here:
myFunction ("Steve", 10, true);
You can also use parentheses to override ActionScript's order of precedence or to make your ActionScript statements easier to read. See Operator precedence.
You also use parentheses to evaluate an expression on the left side of a dot in dot syntax. For example, in the following statement, the parentheses cause new color(this)
to evaluate and create a new color object:
onClipEvent(enterFrame) { (new Color(this)).setRGB(0xffffff)); }
If you didn't use parentheses, you would need to add a statement to the code to evaluate it:
onClipEvent(enterFrame) { myColor = new Color(this); myColor.setRGB(0xffffff); }
Uppercase and lowercase letters
Only keywords in ActionScript are case sensitive; with the rest of ActionScript, you can use uppercase and lowercase letters however you want. For example, the following statements are equivalent:
cat.hilite = true; CAT.hilite = true;
However, it's a good habit to follow consistent capitalization conventions, such as those used in this book, to make it is easier to identify names of functions and variables when reading ActionScript code.
If you don't use correct capitalization with keywords, your script will have errors. When Colored Syntax is turned on in the Actions panel, keywords written with the correct capitalization are blue. For more information, see Keywords and Highlighting and checking syntax.
In the Actions panel, use the comment
statement to add notes to a frame or button action when you want to keep track of what you intended an action to do. Comments are also useful for passing information to other developers if you work in a collaborative environment or are providing samples.
When you choose the comment
action, the characters //
are inserted into the script. Even a simple script is easier to understand if you make notes as you create it:
on(release) { // create new Date object myDate = new Date(); currentMonth = myDate.getMonth(); // convert month number to month name monthName = calcMonth(currentMonth); year = myDate.getFullYear(); currentDate = myDate.getDat (); }
Comments appear in pink in the Script window. They can be any length without affecting the size of the exported file, and they do not need to follow rules for ActionScript syntax or keywords.
ActionScript reserves words for specific use within the language, so you can't use them as variable, function, or label names. The following table lists all ActionScript keywords:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
For more information about a specific keyword, see its entry in the ActionScript Dictionary.
A constant is a property whose value never changes. Constants are listed in the Actions toolbox and in the ActionScript dictionary: Overview in all uppercase letters.
For example, the constants BACKSPACE
, ENTER
, QUOTE
, RETURN
, SPACE
, and TAB
are properties of the Key object and refer to keyboard keys. To test whether the user is pressing the Enter key, use the following statement:
if(keycode() == Key.ENTER) { alert = "Are you ready to play?" controlMC.gotoAndStop(5); }