Learning About Classes, Methods, and Properties


ActionScript 2.0 and Flash 8 Basic are both based on a functional model that involves classes, objects, and instances of objects. While you don't have to know everything about how this model works, it certainly helps to have an understanding of the concepts behind it, because it touches nearly every aspect of how Flash 8 and ActionScript 2.0 work (and work for you). Besides, the more you know about it, the easier it is to boss your application around!

First, you need to understand a class. A class is a collection of scripts that define common capabilities and properties. Classes are important, because they prevent developers from having to recreate lines of code every single time they need to do the same thing in the same or different applications. You can think of them as templates for functionality, and everything around you, in programming, Flash, and real life, belongs to a class of some sort or another.

For example, if you're a dog lover, you might have gone out one day and adopted a puppy. Puppies belong to the Dog class, which is the master template for every dog ruining flower beds on the planet Earth. You know a dog is a dog because all dogs have similar properties and behave in a particular way, from smallest to biggest. When you go and get a puppy from a breeder or a pet store, you have instantiated the dog class (that is to say, made an instance of it, although technically 'twas the momma dog that made the puppy instance), and therefore have a dog object. The puppy is built from the dog template, or dog class, and inherits all of the methods and properties that dogs come with at birth. And you just thought you were getting an enthusiastic friend!

All dogs have methods in common, such as bark() and slobber() and run(). A method is what something does. They also have similar properties: coatType, size, weight, gender, name, and so on. Properties describe the physical structure of an object. When you train a dog, you are adding methods to it; when you groom a dog, you are altering its properties. However, calling methods and changing properties don't change the fundamental structure of the dog, so no matter what you do to it or what you teach it, it is still and always will be a dog. Got it? Good. Because if you want to start changing the fundamental nature of objects like dogs, you'll have to start creating your own classes; fortunately, however, in Flash, you won't have to go to that extreme, because chances are Flash already has classes that handle the tasks you want to accomplish.

Note

If you want to create your own classes, they must be created in external files with an .as extension that are compiled into the SWF file when you publish it. You won't create any classes for this book, but instead just use what are called "built-in classes" that are included in ActionScript itself. All those classes are also stored externally in .as files in Flash's install directory. Feel free to look at them, but don't change anything!


ActionScript 2.0 includes a number of built-in classes, such as the Math class, Button class, and Date class. You have already used the MovieClip class, and some of its methods and properties, which include methods such as stop, and gotoAndPlay. The MovieClip class also has properties you can change or set for an instance, such as _visible, _width, and _height. Heck, your Stage is actually an instance of the MovieClip class. You will look at how to use a built-in class shortly.

Objects based on the MovieClip, Button, TextField, and Component classes that come with Flash can be created (otherwise know as instantiated) visually. However, many Flash classes, such as LoadVars and Sound, must be instantiated using ActionScript. When you create a new instance of a class using ActionScript, you are really calling a constructor function, which is a special kind of function defined in a class that assembles the object and sends it out into memory. Kind of like an assembly line in an automobile factory. To create a new object instance using ActionScript, you write a line of ActionScript similar to the following:

var rockinSound:Sound = new Sound();

In this example, you are creating (or constructing) a new instance of the Sound class giving it the instance name rockinSound, so that you can refer to it when you need it. The Sound instance is stored in memory, so to use it, you have to call it by its name. If you went to pick up your kids from day care, you wouldn't walk into the building and shout "Hey kid, let's go!" because you might wind up with more kids than you bargained for. Or a few kids. Or no kids at all. You have to shout "Hey Cheyenne!" to gather your unruly child (or niece in this case) and hurry along your way. Unpredictable results when gathering children from day care are bad results, and likewise just as bad in ActionScript. "Hey Sound!" has no meaning to Flash. So you name the instance of the Sound class, then call it by name when you want it to do something. Just like a movie clip on the Stage is an instance (or copy) of a movie clip symbol in the library, rockinSound is an instance of the Sound class, only in memory, waiting to do your bidding.

The Sound class is an example of a built-in class. "Built-in" means that the class shipped with Flash, was installed on your computer, and is written with the ActionScript language. The class also has predefined methods and properties that create the class itself, produced by the kind programmers at Macromedia, so you have one less thing to worry about.




Macromedia Flash 8. Training from the Source
Macromedia Flash 8: Training from the Source
ISBN: 0321336291
EAN: 2147483647
Year: 2004
Pages: 230
Authors: James English

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