What Are ActionScript Objects?

[ LiB ]

The generic ActionScript object is at the root of the ActionScript hierarchy. But what is this great object that I'm talking about? An object is an entity in Flash that can have properties and methods , or functions that act upon those properties (or variables ). A specific object of any kind is said to be a class . ActionScript allows you to create any type of class that you wish.

Before continuing, let me just tell you that most structures in ActionScript derive from the generic objecteven the Movie Clips themselves . This is so much so that even the arrays you created in the last section derived from an object.

What do I mean by derive ? What I mean is that the objects I've discussed have the same methods and properties as a generic ActionScript object.

Objects are great because you can also assign new methods and properties to them. The ability to do this to your object is part of OOP, or object-oriented programming. This style of programming is usually a mystery for many newbies, but the truth is that it was created so that you can visualize your objects in the program. See Figure 5.7 for a visual.

Figure 5.7. Visualizing a generic object


What in the world did I mean by that last sentence ? Well, allow me to throw an example at you. Let's say you had a tank main character in your top-scrolling action-adventure. You would want the tank to have a fire function. It is specific to tank objects because they all fire the same (hypothetically) but this same function (method) wouldn't work with another object because it's not part of that object, only part of tank. However, you can have another fire function defined for another objectand make it behave completely differently from the fire function in the tank class. Once the code is written, you would be able to write something like this:

 tank.fire(); Enemy.fire(); ... Enemy.energy = powerUp; 

Notice that both functions have the same name . They each belong to different objects. This makes sense because your enemy might fire different missiles from your tank character.

Now look at your enemy's energy property. It's really just a built-in object variable. It wasn't declared in the environment but as part of the Enemy object. This object could easily be a Movie Clip or even a generic object. How would you create a property just like the energy property? Just like you would any other variablethe only difference is that you aren't creating it under the _root , you are creating it as part of an object, so you have to use dot notation unless you are in the object already. In this case, Enemy was the object I used to declare the energy variable in.



It's best to think of OOP as a sim pler way to organize your program. After you get used to OOP, you're going to get so close to it that you won't want to program in any other way. (Actually, you've been using OOP all this time in this book.)

What I just explained in the previous paragraph regarding personally hidden properties and methods has a name: encapsulation . I didn't throw this name at you before because I don't want you to focus on the vocabulary at this pointI wanted you to grasp the concepts first.

[ 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