What Objects are and Why They''''re Useful - Pdf 68



< 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.

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 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


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status