Tải bản đầy đủ (.pdf) (5 trang)

What Objects are and Why They''''re Useful

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (27.58 KB, 5 trang )



< Day Day Up >

What Objects are and Why They're Useful
ActionScript objects allow you to perform all sorts of interactive tasks with Flash. They
provide a means for you to work with text, sound, color, dates, and more, in very
dynamic ways.
As you'll soon learn, ActionScript objects, while intangible, are very much like their
physical counterparts. They have characteristics known as properties, which can be
changed with a script, and they have abilities, known as methods, which allow them to
perform various tasks. We'll discuss both of the aspects of objects in depth in a moment.
The primary benefit of using objects in ActionScript is that they allow you to program
and manipulate data, colors, sound, dates, et cetera, in a context that makes sense to
humans—we're all familiar with the idea of objects having characteristics and abilities.
Understanding the Concept of Object Classes
At the introduction of this lesson, when we introduced the phrase, "classes of objects,"
you probably scratched your head wondering what in the world the phrase meant. It's
actually a simple yet important concept to understand in the world of object-oriented
programming. We'll touch on it briefly here to acquaint you with its meaning. A more in-
depth discussion can be found in Lesson 7
, "Creating Custom Classes," where you'll
create your own custom classes of objects.
"Classes of objects" (or object classes, or simply classes), is an organizational phrase
used to denote sets of objects with similar characteristics and abilities. You've probably
heard the terms upper class, middle class, or working class to describe groups of people
that fit a certain mold due to their finances or capabilities. The same general idea applies
to the concept of classes of objects in ActionScript. Each object you use in ActionScript
belongs to a specific class that defines the general characteristics and abilities of the
objects in it. To help you grasp this concept in the realm of ActionScript, let's look at an
example.


Perhaps you've noticed that all movie clip instances you place in your project have a
_width or _alpha property (among a lot of other common properties), or that you can
control every movie clip instance's timeline using the gotoAndPlay() action (also known
as a method). This is because Flash contains a MovieClip class (it's hidden from you, but
it exists). This class defines the general capabilities of every movie clip instance you use
in your project. While each movie clip instance might look different, they all have the
same basic properties and abilities. This is similar to how humans walk, talk, and sneeze
(common abilities), but each human's way of doing it is unique. We are all part of the
Human class, so to speak.

It's important to realize that when building your Flash projects, you create and use
instances of various classes of objects, rather than placing the actual class in your project.
It's a subtle but important distinction.
For example, when you drag a movie clip onto the stage, you're actually creating an
instance of the MovieClip class. When you create a dynamic text field, you're creating an
instance of the TextField class. You will usually work with instances (also known simply
as objects) in Flash, as opposed to the actual class (although this can be done, and you
will learn how in Lesson 7
, "Creating Custom Classes"). A class is often referred to as a
blueprint, while an instance of that class is thought of as the resulting usable object that
you work with, based on that blueprint.
As we mentioned, classes of objects are defined by two primary characteristics: their
properties and methods. Let's take an in-depth look at both.
NOTE
In the discussion that follows, the term object refers to an instance of a class. For
example, if we mention an object named mySound, we are referring to an instance of the
Sound class named mySound.

Properties
Many but not all classes of objects have properties—values that represent characteristics

belonging to instances of that class. In the real world, a car has properties like color,
make, model, and horsepower. If your project had a Car class and you had created an
instance of it named myCar, you might access the value of its properties in this manner:

var carColor:String = myCar.color;

var carTopSpeed:Number = myCar.topSpeed;


Several classes of objects in Flash have properties. For example, instances of the
MovieClip class have property values that represent their transparency, visibility,
horizontal position, vertical position, and rotation. Changes to any of these properties
affect the movie clip instance's appearance or functionality, just as giving a car a paint job
or changing its engine would alter the car. You can use property values of various objects
in your scripts to set values elsewhere. Assume that a script in your project moves your
car at a speed based on its horsepower. That line of script might look like this:

var speedFactor:Number = myCar.horsepower;


Here, the value of speedFactor is dependent on the value of the horsepower property of
myCar.
Let's look at one more example of a property and how it's used in ActionScript.
The length of a string is a property of a String object. For example, the length of the term
"Flash" is 5 because it contains five characters. In ActionScript, this would be written like
this:

var name:String = "Flash";

var lengthOfName:Number = name.length;



The first line of code creates a variable called name whose value is the string "Flash".
The second line creates a variable called lengthOfName whose value is that of the length
property of the name object (5). Although property names associated with movie clips are
usually preceded by an underscore (_alpha and _rotation, for example), the property
names of most objects are not preceded by an underscore. MovieClip properties break
from what is considered the norm because their properties were first introduced in Flash
4, when ActionScript had a much different form than it has today.
Methods
A method represents a task that instances of a class of objects can perform. If you think
of VCRs as a class of objects, methods of that class would include the abilities to play,
record, stop, rewind, fast forward, and pause. The syntax representing the methods of our
VCR class would look like this:

play();

rewind();

record();


In order for an instance of the VCR class to invoke (use) a method of its class, the syntax
requires that you first indicate the name of the VCR class instance, followed by a dot and
the name of the method:

myVCR.record();


This tells the VCR instance named myVCR to start recording.

The parentheses included with the method sometimes allow you to invoke the method in
a unique way using a parameter or set of parameter values. Using the VCR example
again, let's say you wanted to record a TV show on Channel 8 from 10:00 p.m. to 11:00
p.m. on September 9. The script required to perform this task might look like this:

myVCR.record("8", "10:00 pm", "11:00 pm", "September 9");


Commas separate the method parameters. Keep in mind that parameter values can be
hard coded as shown, or they can be dynamic values such as variables. You can even use
other methods as parameters. (You'll learn more about parameter values in the next
lesson.)
Although many ActionScript object classes have methods that accept parameters, not all
do. Some methods simply perform tasks that don't require special settings. For example,
the stop() method of the MovieClip class allows you to stop the playback of a movie clip
instance—nothing more, nothing less, thus additional parameters are unnecessary.
Each class of objects has a unique set of methods—it makes sense because each has a
specific function.

The various methods used by object classes in ActionScript perform all sorts of tasks,
including:

Getting and setting values

Doing conversions (for example, converting a negative number to a positive)

Indicating whether something is true or false

Activating or deactivating something


Manipulating something (such as text or numbers)
We'll demonstrate some of these tasks in this lesson and many others throughout the
book.

< Day Day Up >

×