Learning ActionScript 2.0 in Adobe Flash |
|
|
|
| Syntax and Language Fundamentals > About language punctuators > Semicolons and colons | |||
ActionScript statements terminate with a semicolon (;) character, as demonstrated in the following two lines of code:
var myNum:Number = 50; myClip._alpha = myNum;
You can omit the semicolon character and the ActionScript compiler assumes that each line of code represents a single statement. However, it is good scripting practice to use semicolons because it makes your code more readable. When you click the Auto Format button in the Actions panel or Script window, trailing semicolons are appended to the end of your statements by default.
|
NOTE |
Using a semicolon to terminate a statement allows you to place more than one statement on a single line, but doing so usually makes your code more difficult to read. |
Another place you use semicolons is in for loops. You use the semicolon to separate parameters, as shown in the following example. The example loops from 0 to 9 and then displays each number in the Output panel:
var i:Number;
for (i = 0; i < 10; i++) {
trace(i); // 0,1,...,9
}
You use colons (:) in your code to assign data types to your variables. To assign a specific data type to an item, specify its type using the var keyword and post-colon syntax, as shown in the following example:
// strict typing of variable or object
var myNum:Number = 7;
var myDate:Date = new Date();
// strict typing of parameters
function welcome(firstName:String, myAge:Number) {
}
// strict typing of parameter and return value
function square(num:Number):Number {
var squared:Number = num * num;
return squared;
}
You can declare the data type of objects based on built-in classes (Button, Date, MovieClip, and so on) and on classes and interfaces that you create. In the following snippet, you create a new object of the custom type Student:
var firstStudent:Student = new Student();
You can also specify that objects are of the Function or the Void data type. For more information on assigning data types, see Data and Data Types.
|
|
|
|