Flash CS3 Documentation |
|||
| Learning ActionScript 2.0 in Adobe Flash > Data and Data Types > Organizing data in objects | |||
You might already be used to objects that you place on the Stage. For example, you might have a MovieClip object on the Stage, and this object contains other movie clips inside it. Text fields, movie clips, and buttons are often called objects when you place them on the Stage.
Objects, in ActionScript, are collections of properties and methods. Each object has its own name, and it is an instance of a particular class. Built-in objects are from classes that are predefined in ActionScript. For example, the built-in Date class provides information from the system clock on the user's computer. You can use the built-in LoadVars class to load variables into your SWF file.
You can also create objects and classes using ActionScript. You might create an object to hold a collection of data, such as a person's name, address. and telephone number. You might create an object to hold color information for an image. Organizing data in objects can help keep your Flash documents more organized. For general information on creating a custom class to hold a collection of methods and properties, see Writing custom class files. For detailed information on both built-in and custom classes, see Classes.
There are several ways to create an object in ActionScript. The next example creates simple objects in two different ways, and then loops over the contents of those objects.
// The first way var firstObj:Object = new Object(); firstObj.firstVar = "hello world"; firstObj.secondVar = 28; firstObj.thirdVar = new Date(1980, 0, 1); // January 1, 1980
This code, which is one way to create a simple object, creates a new object instance and defines a few properties within the object.
// The second way
var secondObj:Object = {firstVar:"hello world", secondVar:28, thirdVar:new Date(1980, 0, 1)};
This is another way of creating an object. Both objects are equivalent. This code above creates a new object and initializes some properties using the object shorthand notation.
var i:String;
for (i in secondObj) {
trace(i + ": " + secondObj[i]);
}
firstVar: hello world secondVar: 28 thirdVar: Tue Jan 1 00:00:00 GMT-0800 1980
|
NOTE |
|
The variables might not necessarily appear in this order in the Output panel. You cannot guarantee the ordering when you use a for..in loop; the player returns the contents of an object in an unpredictable order. |
You can also use arrays to create objects. Instead of having a series of variables such as firstname1, firstname2, and firstname3 to represent a collection of variables, you can make an array of objects to represent the same data. This technique is demonstrated next.
var usersArr:Array = new Array();
usersArr.push({firstname:"George"});
usersArr.push({firstname:"John"});
usersArr.push({firstname:"Thomas"});
The benefit of organizing variables into arrays and objects is that it becomes much easier to loop over the variables and see the values, as shown in the following step.
var i:Number;
for (i = 0; i < usersArr.length; i++) {
trace(usersArr[i].firstname); // George, John, Thomas
}
George John Thomas
The following example presents another way to loop over objects. In this example, an object is created and looped over using a for..in loop, and each property appears in the Output panel:
var myObj:Object = {var1:"One", var2:"Two", var3:18, var4:1987};
var i:String;
for (i in myObj) {
trace(i + ": " + myObj[i]);
}
//outputs the following:
/*
var1: One
var2: Two
var3: 18
var4: 1987
*/
For information on creating for loops, see Using for loops. For information on for..in loops, see Using for..in loops. For more information on objects, see Classes.
Flash CS3