What Is a Class?

What Is a Class?

Every object instance in Flash has an associated class. A class is a description of methods and properties that serves as a blueprint for how instances should behave. All Action- Script instances are tied to their class the way all symbol

Figure 3.1 Dragging a symbol from the Library to

instances on the Flash Stage are tied to their Library item.

the Stage will create a new instance of the symbol.

When a change is applied to a class, it affects all instances of that class.

The purpose of a class is determined by its methods and properties. The methods found in a class describe what the class does. Methods are generally associated with actions, such as go, find, hide, jump, and so on. The properties of a class describe characteristics that the class possesses. Prop- erties are generally associated with attributes or features, such as visible, height, position, color, and so on. Some properties can have new values assigned to them, and some properties cannot be changed. Properties that cannot be changed are referred to as read-only.

The Object class is actually the most basic class in Action- Script. All classes inherit their basic methods and prop- erties from the generic Object class. Inheritance is the mechanism that allows for the reuse of behaviors and traits (methods and properties) without writing new code.

You may remember a chart from Biology class that showed single-celled organisms (like protozoa) from billions of years ago at one end and complex multicellular organisms

Chapter 3 Introduction to ActionScript Classes

(like humans) of our contemporary age at the other. The progression from simple to complex occurred because nature tended to (and needed to) solve basic problems of survival and reproduction before tackling something as complex as the human eye. At the same time, com- plex organisms still benefit from the early mechanisms developed in protozoa. The Object class is much like the protozoa—simple, basic, and tackles the most essential problems only.

You can locate the inheritance On the other end of the complexity spectrum, the chain for a class in the Flash Help documentation. Choose Flash >

MovieClip class is responsible for much of what is Help > Flash Help. In the Adobe seen in Flash. The MovieClip class is much more com-

Help application, select Action- plex than the Object class because it is the result of

Script 3.0 and Components

a much longer chain of inheritance—MovieClip > under the Adobe Reference heading. On the next page, select Action-

Sprite > DisplayObjectContainer > InteractiveObject > Script 3.0 Reference for the Adobe DisplayObject > EventDispatcher > Object.

Flash Platform . Select a class from the bottom-left side of the window.

Observe the trend from general to specific within the The top of each class page lists a MovieClip’s inheritance chain. Inheriting directly from the

class’s package and inheritance. Object class, an EventDispatcher is an object that is capable of sending and receiving events (we’ll cover events later in the chapter), but it doesn’t do much more than that. Send- ing and receiving information is important to a number of other classes, so the EventDispatcher class is rightfully low in the inheritance chain. At any point in the chain, new limbs can branch out in different directions, leading to a Microphone class or a URLLoader class, but in the case of the MovieClip chain, it branches toward a limb for display- ing objects onscreen.

The MovieClip class is built into the core functionality of Flash, but you can also write classes of your own that have brand-new functionality. New classes that you write are stored externally as ActionScript (.as) files. Your classes can inherit from built-in classes like MovieClip, they can inherit from other classes that you’ve written, or they can

be written entirely from scratch (and by default, they will then inherit from the Object class). The process of inher- iting from another class is also referred to as extending a class, because you are extending the functionality of an existing class. The extending class is considered a subclass of the class that it extends.

Animation with Scripting for Adobe Flash Professional CS5 Studio Techniques

Understanding the trend from general functionality to very specific functionality is the basis for designing useful ActionScript classes. Writing a reusable class begins with considering how the desired functionality can be made more general or abstract. Later in the chapter you’ll learn

how to write a class that controls an animated character’s walk cycle. To make your character walk, you could start by writing very specific code for the task, or you could take a moment and consider how this behavior could be made more abstract. You can start by asking the following questions: What’s more primitive than walking? How about just moving? Moving also applies to fish, birds, and snakes as well as to walking characters. By writing a Mover class first, you’ll be able to use your code for any type of mov- ing object, be it a Pac-Man-style character, a side-scrolling Super Mario-style character for an adventure game, or a paddle for a simple Pong-style game. Once written, your Mover class never has to be rewritten. When you need new functionality, you can simply extend the Mover class.

Reusability is only one of many organizational benefits to writing code in classes with clearly defined behaviors. Consider this situation: You’ve been given a project that was designed by someone else. You’ve been tasked with updating some of the graphics and functionality. Which of the following would you rather see when you open the files: A) One 600-line ActionScript file with a list of direc- tions for vaguely named instances on the stage, or B) four short ActionScript files named Paddle, Ball, Background, and Controller? In B, you have a pretty good idea of what the project entails just by looking at the names of the files. Every object has its own behaviors, and since the objects are clearly named, it’s easy to intuit what those behav- iors might entail. This kind of organization also pays off when you need to work on your own files that you haven’t opened in a long while.

In addition to understanding how classes work, there are also some basic programming terms that you need to know to write ActionScript code. We’ll define new terms as they appear in the examples, but you can refer to Table 3.1 and Table 3.2 as guides for the basic definitions and syntax of common programming terms.

Chapter 3 Introduction to ActionScript Classes

T ABLE 3.1 Definitions for basic programming terms

T ERM

D EFINITION

Variable A named object with an associated value that can be changed

Function A portion of code that performs a specific task Method

A function associated with a particular object Parameter

A piece of data that can be used within a function Argument

A parameter that is sent to a function Loop

A piece of code that is repeatedly executed Array

A collection of objects

T ABLE 3.2 Syntax examples for basic programming terms

T ERM

E XAMPLE

Variable

var myNumber = 10;

Function function myFunction(){} Method

function myMethod(){} Parameter

function myMethod( myParam ){}

Argument

myMethod( myArg );

Loop

for(var i = startNumber; i <= endNumber; i++){}

Array

myArray = [myVarA, myVarB, myVarC];

The best way to learn the terms in the preceding tables is to start using them. Before you put them into action, let’s go over some words and symbols that ActionScript has reserved for special use.