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:

  1. Select File > New and then select ActionScript File.
  2. Select File > Save As and save the new document as Student.as.
  3. Add the following ActionScript to the AS 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;
        }
        // ...
    }
    
  4. Save the class file.
  5. Select File > New and click Flash Document to create a new FLA file.
  6. Save the new FLA file as student_test.fla.
  7. Type the following ActionScript on Frame 1 of the main Timeline:
    // 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
    
  8. Select File > Save to save the changes to student_test.fla.
  9. Select Control > Test Movie to test the FLA and AS files.

The next example demonstrates how curly braces are used when you work with functions.

To use curly braces with functions:

  1. Select File > New and select Flash Document to create a new FLA file.
  2. Select File > Save As and name the new file checkform.fla.
  3. Drag an instance of the Label component from the Components panel onto the Stage.
  4. Open the Property inspector (Window > Properties > Properties) and with the Label component instance selected, type an instance name of status_lbl into the Instance Name text box.
  5. Type 200 into the W (width) text box to resize the component to 200 pixels wide.
  6. Drag an instance of the TextInput component onto the Stage and give it an instance name of firstName_ti.
  7. Drag an instance of the Button component onto the Stage and give it an instance name of submit_button.
  8. Select Frame 1 of the Timeline, and add the following ActionScript into the Actions panel:
    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);
    
  9. Select File > Save to save the Flash document.
  10. Select Control > Test Movie to test the code in the authoring environment.

    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.