Object Statements
This section covers a couple of the statements associated directly with objects. These include the with statement and the for in statement.
The with Statement
The with statement is for controlling multiple properties or methods of an object without the hassle of retyping the object over and over. Just place the object's name in the parentheses, and the properties and methods listed between the brackets are the ones affected for that object. Here's an example:
Another use of the with statement involves associating it with a movie clip and using some of the drawing API features available in Flash.In this example, you create an empty movie clip on the main stage; then, using the onEnterFrame event, you'll make it possible for a line to be drawn wherever the mouse goes. You will also use a simple Math.random() method to make the line change color constantly to add a little style.
//first create an object
var myObject:Object = new Object();
with (myObject){
//set 2 properites
height = 50;
width = 100;
//call a method
setName("Rectangle");
}
As you can see, using with allows you to associate the drawing actions with a single movie at once, instead of having to associate each action individually, like this:
//create the empty movie clip
this.createEmptyMovieClip("line_mc",1);
//now the event that will allow a line to follow the mouse
line_mc.onEnterFrame=function(){
with(line_mc){
lineStyle(5,Math.random()*0x10000000,100);
lineTo(_xmouse,_ymouse);
}
}
As you have seen, the with statement can be very powerful, especially if it's used in conjunction with a function, as in the following example:
//create the empty movie clip
this.createEmptyMovieClip("line_mc",1);
//now the event that will allow a line to follow the mouse
line_mc.onEnterFrame=function(){
line_mc.lineStyle(5,Math.random()*0x10000000,100);
line_mc.lineTo(_xmouse,_ymouse);
}
Now, all you have to do is call the function with any movie clip and all the properties and methods associated with the with statement will be applied to that clip.
function myFunction (myMovie:MovieClip){
with (myMovie){
_x=50;
_y=20;
trace (myMovie._name);
}
}
The for in Statement
The for in statement is an advanced loop statement that's associated directly with objects. Unlike other loop statements, which run based on a defined condition, the for in statement runs until all properties of the assigned object are evaluated.The syntax for this statement can be difficult, so it's important that you read this section carefully. Start with the for keyword; then add an opening parenthesis and the keyword var. Following that, name the variable that will hold each property name for the object; then add the keyword in. Next, place the name of the object you are using followed by a closing parenthesis and an opening bracket. Between the brackets is where you'll place the code that will use the properties of the object. Let's take a look at a generic template:
This seems simple enough, so now let's go over how to use the properties. There are two types of property calls: one calls the property's name, and the other calls the property's value.The first type of property call uses the variable you created to hold the property names. An example will help make this clearer.First, let's create an object we can use for the rest of the exercise; then we'll use the for in statement to call each property's name in this object:
for (var myProp in myObject){
//the code to use the properties;
}
In the preceding example, we traced the variable we created to hold each property in our object. As you'll notice, it does not start at the beginning, but rather at the end, and it moves toward the beginning.Now that you know how to pull the names of the properties, let's go over how to pull the values of each property. To get the value of each property, use the object's name (in this case, contact) and connect it to the variable you created inside of brackets. Here is the example:
var contact:Object = new Object();
contact.name = "David";
contact.age = 25;
contact.state = "VA";
for (var myProp in contact){
trace (myProp);
}
//output: state
// age
// name
You know how to get the names of the properties, and you just saw how to get the values. Now let's combine the two:
var contact:Object = new Object();
contact.name = "David";
contact.age = 25;
contact.state = "VA";
for (var myProp in contact){
trace (contact[myProp]);
}
//output: VA
// 25
// David
Let's not stop there; let's take a big step forward and set the for in statement to a function. Then you'll place all the properties of the object in an array as named elements (see Chapter 10, "Arrays," for more on arrays):
var space:String = " "
var contact:Object = new Object();
contact.name = "David";
contact.age = 25;
contact.state = "VA";
for (var myProp in contact){
trace (myProp + ":" + space + contact[myProp]);
}
//output: state: VA
// age: 25
// name: David
While we are on the subject of arrays, note that you can also pull each element out of an array using the for in statement, as if it were a property of an object. Here's an example:
var contact:Object = new Object();
contact.name = "David";
contact.age = 25;
contact.state = "VA";
var my_array:Array = new Array();
function makeArray (myObject:Object, arr:Array):Void{
for (var myProp in myObject){
arr[myProp] = myObject[myProp];
}
}
makeArray(contact, my_array); //call the function
trace (my_array.name);
//output: David
The for in statement also works on named array elements, as you can see here:
var my_array:Array = new Array ("David",25,"VA");
var space:String = " ";
for (var element in my_array){
trace (element + ":" + space + my_array[element]);
}
//output: 2: VA
// 1: 25
// 0: David
Now that we have discussed object statements, let's move on to flow modifiers.
var my_array:Array = new Array ("David",25,"VA");
my_array.city = "Richmond";
var space:String = " ";
for (var element in my_array){
trace (element + ":" + space + my_array[element]);
}
//output: city: Richmond
// 2: VA
// 1: 25
// 0: David