What Is an Object? The Basics


In ActionScript, an object is simply an entity that can have properties and methods attached to it. You've already seen this many times in the past when we attached new variables and functions to a movie clip, for example. Objects work the same way except that they are not displayed on the stage. Instead, they exist only in your ActionScript code.

Objects have types, just like variables. You've seen variables that are numbers , strings, and booleans. Objects have types as well, such as Array , Number , TextField , and MovieClip .

Custom-Made Object Types

In addition to the objects that Flash comes with, we can make our own. Custom objects are powerful because they allow us to encapsulate all the methods and properties related to an element of our application into one place. In Chapter 9, we are going to look at making a few alien objects that manage how aliens move, attack, and die. It certainly wouldn't make sense for Flash to ship with an object that would have little relevance to most Flash developers.

Instead, Macromedia decided to ship with core ECMA objects and some additional objects that only have relevance in Flash, such as the MovieClip object. For any additional objects that we need for a specific application, we are left with the freedom to create our own. For now, let's finish looking at the built-in objects so that we know what tools are already available to us.

Global Objects Versus Instance Objects

There are two main branches of objects in Flash: global objects and instance objects. You cannot create instances of a global object. Instead, one object is already created for you, and you use that object in any timeline. The Math object is an example of a global object. Notice the way the following script uses the Math object as a reference for the methods:

 Math.cos(x); Math.sqrt(56.23); 

We're calling methods on an object named Math , but you cannot create instances of the Math object. That is not true of all objects, however.

Instance objects are used to create instances of themselves . This is called a class. Examples of classes are Array , TextField , and Color . You don't use these objects except to create new instances of your class, as in the following script, which creates a new instance of the Array object:

 myArray = new Array(); 

The script creates a reference and assigns it a new object. The object is then created with a call to the new operator, which is followed by the object name with parentheses. Let's break this down into pieces.

The new Operator

You've seen the new operator used in previous chapters, but I want to define it again here. The new operator is used to create a new instance of some object. It essentially tells Flash that what follows is an instance object name, and we want a new instance of it.

The parentheses that follow the object name in the preceding code are a function call to what is known as a constructor. Constructors are covered in the next section.

Be careful with new ; you can create instances implicitly without using new or a constructor call. In fact, we've done this before. Consider the following script, which first appeared in Chapter 5, "Arrays: Match 'Em Up and Sliders ":

 myArray = ["apple", "banana", "carrot"]; 

Constructors

You saw constructors with arrays in Chapter 5 as well as earlier in this chapter. After the new keyword, the class name (type) is given followed by a set of parentheses as in myArray = new Array(); . The parentheses contain a set of arguments given to the constructor that set up the object. They can be used with and without arguments. The following script shows several different object types being created with constructors:

 b = new Boolean(false); s = new String("hello"); n = new Number(10); a = new Array(); 

The idea is that you are giving data to the object's constructor, whose job it is to set up your object instance for you to use. Sometimes you want the object to be customized a bit, and in cases like that, you can usually give arguments to this constructor function to create an object to your specification.

The typeof Operator

While we're on the subject of objects and types, we should talk about the typeof operator. Oddly enough, this operator is actually used like a function. You follow the operator with a set of parentheses containing one variable, and the operator returns the type of the variable. Consider the following script:

 b = new Boolean(false); n = new Number(10); a = new Array(); f = function(){} x = 10; trace(typeof(b)+" "+typeof(n)+" "+typeof(a)+" "+typeof(f)+" "+typeof(x)); 

The output of this script is shown in Figure 6.1. Notice that the Boolean , the Number , and the Array have output of object . However, the function has output of function and x has output of number .

click to expand
Figure 6.1: The Boolean , Number , and Array reference are of type object . The function is of type function , and the primitive, x , is of type number .

These traced values occur because typeof gives you object as its output if the variable refers to any object. Because f is a function, typeof says it's a function. Finally, x was a primitive numerical type, so Flash outputs number .

The delete Operator

Sometimes you will create an object and later realize you don't need it anymore. The problem is that the object is still sitting in memory, taking up space. Flash gives you a way to remove unneeded objects from memory: the delete operator. The following script demonstrates delete 's use:

 myArray = new Array("a"," b"," c"); trace(myArray); delete myArray; trace(myArray); 

The first output will be the values of the array elements separated by commas because myArray refers to a newly created array. However, the second output, which follows the delete , will output undefined because myArray no longer refers to an object. Instead, myArray is not a valid reference anymore, so the trace returns undefined .




Macromedia Flash MX 2004 Game Programming
Macromedia Flash MX 2004 Game Programming (Premier Press Game Development)
ISBN: 1592000363
EAN: 2147483647
Year: 2004
Pages: 161

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