We've really come a long way since Chapter 1, "A Gentle Introduction for Non-Programmers". Our first exposure to the components of ActionScript was a simple perusal of the items under the + button in the Actions panel. Since then, we've learned about 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. These are tools, but they are just tools. They're the grammar we use to compose instructions in our scripts. What we're still missing is subject matter. By now we know perfectly well how to speak ActionScript, but we have nothing to talk about. The built-in classes and objects of ActionScript fill that void.
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 the 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 would set the color of square to gray.
Because the Color object is a built-in class, it can directly set the color of a movie clip. Still other classes give us control over sounds, the date, and XML documents. We'll learn all about the built-in classes in Part III, "Language Reference".
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 are always 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 Part III, "Language Reference" we'll learn about the other built-in objects -- Math, Mouse, and Selection -- which provide access to mathematical information, the mouse pointer, and text field selections.
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 Part III, "Language Reference". However, it's not necessary to study all the classes and objects in one sitting. Learn about movie clips to start and then 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.
Copyright © 2002 O'Reilly & Associates. All rights reserved.