Using Objects

[ LiB ]

If you are already familiar with ActionScript, you would have noticed that I have so far failed to mention one type of loop. I've waited this long because I wanted to introduce both arrays and objects so that you'd have a nice grasp of what's going on. This new loop is called a for-in loop, and is a more convenient way to traverse objects and arrays, as we'll soon see.

The for-in loop has very simple syntax. It is written in the following format.

 for (var element in object) {   object[element]; } 

As you can see, when the loop initializes itself, it creates a variablein this case called element . This loop will iterate through all the properties in the object (or elements in an array) and you can use the combination of the object and loop variable to extract the value of the current elementall you have to do is use the loop variable as a reference, or visually, something like this:

 object["element"]; 

NOTE

TIP

Remember that anything I show you in this section can be applied to arrays in one way or another including this new for-in loop. Just remember that when you iterate through an object, the loop starts at the top, or end of an array, and ends at the bottom or the first element.

In order to iterate through each element in an object you can write up a for-in loop like this:

 for (var attr in spaceShip) {   trace("spaceShip's " + attr + "contains the value of " + spaceShip[attr]); } 

What determines the number of iterations is the number of properties in the object in question.

Let's dive into the statement and see how it works. Besides the keyword for , we see the usual parenthesis. What I did first was declare a variable that the for-in loop will usea local variable named attr . Following this is the in keyword. The spaceShip object follows the keyword. After that comes the closing parenthesis and the body. What this statement is basically saying is, "For every property in spaceShip , loop once."

Why did I declare attr? Because during each respective iteration, the next property is assigned to attr as a string value. This will allow me to see which property I am working with. For instance, if on the third iteration the property is Ammo , the attr variable will contain "Ammo" as a string value.

I have used a trace command in the body of the for-in loop so you can better understand what I am talking about.

NOTE

NOTE

The trace command probably looks a little complicated.The only reason this is so is because I'm concatenating four strings in one statement.The first one is spaceShip's . I am then adding this to the string in attr .This string is then added to contains the value of . And finally, the value that is returned by spaceShip[attr] is also attached to the end of the string and outputted to the window.

The trace command is pretty much self-explanatory except for the last piece, spaceShip[attr] . This last reference to a property can seem a little confusing because there are no quotation marks around attr . You cannot use quotation marks or else ActionScript will be looking for the attr property within spaceship , and it doesn't exist. attr is merely a temporary variable that is being used in the for-in loop. Remember that when attr is referencing Ammo , it will be in string format. If you rewrite spaceShip[attr] at that stage, it will become clear why I wrote it like this: spaceShip["Ammo"] . This reference to the property will return the value stored within the Ammo property of spaceShip . See how powerful this flexibility is?

A sample output would be this:

 spaceShip's Ammo contains the value of 50 

When using objects, you can also create your own class as you would define a function. The name of the structure that creates these classes is called a constructorjust like new Array() and new Object() are constructors.

Figure 5.8. Creating a class from a generic object

graphic/05fig08.gif


NOTE

TIP

An object class is simply a category that you can set upit's another OOP thing. For instance, you can set up a spaceship class, animal class, human class, and so on. Keep reading and you'll see how you can create object classes.

 function spaceShip() {   this.Ammo = 50;   this.Speed = 25; } 

Here, you are essentially creating a class with two properties, Ammo and Speed . You can then use your favorite new keyword to create a new object of this class.

 XShip = new spaceShip(); 

XShip is automatically initialized with the values you declared earlier in spaceShip . Isn't this cool?

But what if you want to assign a new method, such as the walk method you assigned to the dog earlier?

 function spaceShip() {   this.Ammo = 50;   this.Speed = 25;   this.Shoot = shootingFunction; // This function was defined somewhere else. } 

After defining the class like this and declaring XShip , you should make XShip easily fire by typing in the following line:

 XShip.Shoot(); 

Additional methods can be assigned in a similar manner. The declaration definition for assigning methods in a constructor function is this:

 this.method = functionAlias; // functionAlias is defined elsewhere 

or

 this.method = function () { // Function code goes here } 

It's really that simple. Don't forget to unleash your creativity, and don't be intimidated by the second way to assign a method. It's usually only used for shorter functions that you don't want to define elsewhere. This will allow you to skip assigning the method an external name while defining it right in the constructor. If you find the method convenient, use it; if not, just code with what you are comfortable with. ActionScript gives you plenty of choices to code freely .

[ LiB ]


Game Development with ActionScript
Game Development with ActionScript
ISBN: 1592001106
EAN: 2147483647
Year: 2004
Pages: 162
Authors: Lewis Moronta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net