Learning ActionScript 2.0 in Adobe Flash |
|
|
|
| Syntax and Language Fundamentals > About language punctuators > Curly braces | |||
You group ActionScript events, class definitions, and functions into blocks using curly brace ({}) punctuators. You put the opening brace on the same line as the declaration.
|
NOTE |
You can also put the opening brace on the line that follows the declaration. Coding conventions recommend that you put the opening brace on the same line for consistency. For information on braces and code conventions, see Best Practices and Coding Conventions for ActionScript 2.0. |
Place braces around each statement when it is part of a control structure (such as if..else or for), even if it contains only a single statement. This good practice helps you avoid errors in your ActionScript when you forget to add braces to your code. The following example shows code that is written using poor form:
var numUsers:Number;
if (numUsers == 0)
trace("no users found.");
Although this code validates, it is considered poor form because it lacks braces around the statements.
|
TIP |
Braces are added to this statement if you click the Auto Format button. |
In this case, if you add a second statement after the trace statement, the second statement executes regardless of whether the numUsers variable equals 0, which can lead to unexpected results. For this reason, add braces so the code looks like the following example:
var numUsers:Number;
if (numUsers == 0) {
trace("no users found");
}
In the following example, you create both an event listener object and a MovieClipLoader instance.
var imgUrl:String = "http://www.helpexamples.com/flash/images/image1.jpg";
this.createEmptyMovieClip("img_mc", 100);
var mclListener:Object = new Object();
mclListener.onLoadStart = function() {
trace("starting");
};
mclListener.onLoadInit = function(target_mc:MovieClip):Void {
trace("success");
};
mclListener.onLoadError = function(target_mc:MovieClip):Void {
trace("failure");
};
var myClipl:MovieClipLoader = new MovieClipLoader();
myClipl.addListener(mclListener);
myClipl.loadClip(imgUrl, img_mc);
The next example displays a simple class file that could be used to create a Student object. You learn more about class files in Classes.
To use curly braces in an ActionScript file:
// Student.as
class Student {
private var _id:String;
private var _firstName:String;
private var _middleName:String;
private var _lastName:String;
public function Student(id:String, firstName:String, middleName:String, lastName:String) {
this._id = id;
this._firstName = firstName;
this._middleName = middleName;
this._lastName = lastName;
}
public function get firstName():String {
return this._firstName;
}
public function set firstName(value:String):Void {
this._firstName = value;
}
// ...
}
// student_test.fla
import Student;
var firstStudent:Student = new Student("cst94121", "John", "H.", "Doe");
trace(firstStudent.firstName); // John
firstStudent.firstName = "Craig";
trace(firstStudent.firstName); // Craig
The next example demonstrates how curly braces are used when you work with functions.
To use curly braces with functions:
function checkForm():Boolean {
status_lbl.text = "";
if (firstName_ti.text.length == 0) {
status_lbl.text = "Please enter a first name.";
return false;
}
return true;
}
function clickListener(evt_obj:Object):Void {
var success:Boolean = checkForm();
};
submit_button.addEventListener("click", clickListener);
In the SWF file, an error message is displayed if you click the Button instance on the Stage when you do not have text in the firstName_ti TextInput component. This error appears in the Label component and informs users that they need to enter a first name.
The next example using curly braces shows how to create and define properties within an object. In this example, properties are defined in the object by specifying the variable names within the curly brace ({}) punctuators:
var myObject:Object = {id:"cst94121", firstName:"John", middleName:"H.", lastName:"Doe"};
var i:String;
for (i in myObject) {
trace(i + ": " + myObject[i]);
}
/*
id: cst94121
firstName: John
middleName: H.
lastName: Doe
*/
You can also use empty curly braces as a syntax shortcut for the new Object() function. For example, the following code creates an empty Object instance:
var myObject:Object = {};
|
TIP |
Remember to make sure each opening curly brace has a matching closing brace. |
|
|
|
|