Creating and Initializing Objects

[ LiB ]

As with arrays, Objects can be created in a couple of different ways. By understanding the structure of the generic ActionScript Object , you will further understand other derived ActionScript Objects .

I'll first show you the long-hand way to create an object, and then I'll show you some expert shortcuts. Sound good?

Creating an object is not much different from creating an array. Check out the code to see why:

 info = new Object(); 

By writing in this line, you are telling ActionScript that info is now of type Object . That's great, but the object is pretty much void right now. Let's assign some properties to it.

 info.Name = "Lewis" info.Age = "22" 

Coolokay, so we just added Name and Age as part of our info Object . Would you be surprised that you can do exactly the same with an array? As I mentioned before, an array is derived from a generic Object , which is what we created here. This enables us to use the array as a generic object without all the fancy array properties. Interesting.

If you like, you can go ahead and type in this small program and verify that the info was assigned with the trace command. You'll know when something went wrong when you go to output something to the screen and the Output Window is blank.

Now for the "cool" way to declare objects:

 emptyObj = {}; 

This is essentially the same as:

 emptyObj = new Object(); 

What's the advantage over the longer way? It's a huge advantage, actuallyyou can initialize and set properties within those curly brackets. For instance:

 Pos = { x: 50, y: 300 }; 

Doesn't it seem like a completely different language? Who would have thought you would be using the assignment operator in such a way? Did you ever think you would be using a colon as an alternative to assign a value? This line creates an object with two properties, x and y. x is initialized to a value of 50 and y is assigned 300. But where is the assigning happening? Don't be fooled; the colon withinand only withinthe curly brackets behaves like the regular assignment operator. Successive properties are separated by a comma as shown in the example above.

It shouldn't come to as surprise that there is more than one way to refer to an object property. As you become more advanced with the language, and as you get more comfortable with it, you will start to see the real advantage to using this way to reference the object property. Take a look:

 spaceShip.speed = 40; var saveSpeed = spaceShip["speed"]; spaceShip["speed"] = 20; 

Here I presented the usual dot notation and our new notation using the [] operators. The speed property is initialized to 40. It is then saved to saveSpeed and then it is assigned a new value of 20.

Notice that the new notation resembles arraysdon't confuse the two. speed is a property that was created in spaceShip . It is then referenced in quotation marks within brackets following the object alias. Once the property is created, it can also be referenced and assigned new valuesjust like in the last line of my example code.

[ LiB ]

Game Development with ActionScript
Game Development with ActionScript
ISBN: 1592001106
EAN: 2147483647
Year: 2004
Pages: 162
Authors: Lewis Moronta © 2008-2017.
If you may any questions please contact us: