12.8 Built-in ActionScript Classes and Objects

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 12.  Objects and Classes

We've really come a long way since Chapter 1. Our first exposure to ActionScript was a simple perusal of the items in the Actions panel. Since then, we've studied data and expressions, operators, statements, and functions; and now we've explored the concept of classes and objects. It's time to put the final brush strokes on the picture we've been painting of the ActionScript language.

ActionScript comes with a variety of syntactic tools: expressions contain data; operators manipulate data; statements give instructions; and functions group instructions into portable commands. They constitute ActionScript's grammar, but until now, our vocabulary has been very limited. The built-in classes and objects of ActionScript fill that void and give us lots of interesting things to talk about.

12.8.1 Built-in Classes

Just as we define our own classes to describe and manipulate objects created according to our specifications, ActionScript defines its own classes of data. A variety of prefabricated classes, including the Object class, are built right into the ActionScript language. The built-in classes can control the physical environment of a Flash movie.

For example, one of the built-in classes, the Color class, defines methods that can detect or set the color of a movie clip. To use these methods, we first create a Color object using the Color( ) class constructor, as follows:

clipColor = new Color(target);

where clipColor is a variable, array element, or object property that stores our Color object. The Color( ) constructor takes one argument, target, which specifies the name of the movie clip whose color we want to set or examine.

So, suppose we have a movie clip named square, and we want to change its color. We create a new Color object like this:

squareColor = new Color(square);

Then, to set our square clip's color, we invoke one of the Color methods on our squareColor object:

squareColor.setRGB(0x999999);

The setRGB( ) method sets the RGB color value of our Color object's target, which in this case is square. So, the previous method invocation sets the color of square to gray.

Because the Color object is a built-in class, it can set the color of a movie clip on screen. Other classes give us control over sounds, text fields, buttons, the date, and XML documents. You'll learn all about the built-in classes in the ActionScript Language Reference.

12.8.2 Built-in Objects

Like built-in classes, built-in objects let us control the movie environment. The Key object, for example, defines a series of properties and methods that tell us about the state of a computer's keyboard. To use these properties and methods, we don't instantiate a Key object, we simply use the Key object directly. Built-in objects are made automatically by the interpreter when the Flash Player starts, and they are globally available throughout a movie.

Here, for example, we display the keycode of the currently depressed key by invoking the Key object's getCode( ) method:

trace(Key.getCode());

And here, we check whether the spacebar is depressed, using the isDown( ) method and supplying the spacebar's keycode as an argument:

trace(Key.isDown(Key.SPACE));

In the ActionScript Language Reference, you'll learn about the other built-in objects.

12.8.3 Learning the Ropes

Learning to write valid ActionScript code is only half the job of learning to program in Flash. The other half comes with learning the available built-in classes and objects and their many wonderful properties and methods. We'll undertake that task in the ActionScript Language Reference. However, it's not necessary to study all the classes and objects in one sitting. Learn about movie clips to start, and branch out as you need to. Over time, you'll come to know what's essential to get your particular job done. What's important is that you understand the general structure of object-oriented programming. Once you know the rules of the system, learning a new object or a new class is a simple matter of looking up its method and property names.

12.8.4 Adding Custom Methods and Properties to Built-in Classes and Objects

Though the built-in classes and objects are predetermined by ActionScript, we can still give them new abilities by adding custom methods and properties to them exactly as we do with own custom classes and objects. Entirely new classes can also be derived from built-in classes.

The following code adds a randomInt( ) method to the static Math object:

// Returns a number in the range minVal to maxVal, inclusive Math.randomInt = function (minVal, maxVal) {   return minVal + Math.floor(Math.random() * (maxVal + 1 - minVal)); };

The following code adds a scrollToBottom( ) method to the TextField class:

TextField.prototype.scrollToBottom = function ( ) {   this.scroll = this.maxscroll; };

The following code creates a subclass of the XML class:

function CustomXML (src) {   super(src); } CustomXML.prototype = new XML( );

Some built-in classes actually require the execution of their constructor function as part of the setup for new objects. When in doubt, always invoke the superclass constructor as shown in our previous examples. Note that not all built-in classes can be subclassed, because some built-in classes (e.g., TextField, Button) are tied to physical objects on screen. See each class entry in the ActionScript Language Reference for details.

Many, many more examples of extending the built-in classes and objects can be found on the developer community site [PROTO]type:

http://www.layer51.com/proto/
     



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

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