WHAT OBJECTS ARE AND WHY THEY RE USEFUL


WHAT OBJECTS ARE AND WHY THEY'RE USEFUL

Imagine for a moment that you're a carpenter. As such, you use prefab objects to build houses for a living. To build a doorframe, you might use some two-by-fours, a hammer, and a bunch of nails objects that have certain characteristics that allow you to do your job. Sometimes as in the case of a cement mixer you may not even know precisely how an object works: You simply give it one thing, and it spits out another.

You can conceptualize Flash objects in much the same way as these carpenter's objects elements used in a specific way in order to build an application or perform a certain task. If you think of objects as tools and building materials for your project, you can begin to understand their benefits.

ActionScript provides a number of prebuilt objects for various purposes. However, if you can't find a premade object that fits your needs, you can create one of your own and program it to do just what you want it to do.

graphics/04fig02.gif

Objects are defined by two primary characteristics: properties and methods. Let's take an in-depth look at both.

Properties

Many, but not all, objects have properties values that represent an object's characteristics. In the real world, a car has properties like color, make, model, horsepower, and so on. If that car was an ActionScript object, you might define it as follows:

 car.color = "red";  car.make = "Volkswagen";  car.model = "Beetle";  car.horsepower = 200; 

There are several objects in Flash that have properties. For example, the MovieClip object has property values that represent its transparency, visibility, horizontal position, vertical position, rotation, and more. Changing any of these properties affects the movie clip's appearance or functionality, just as giving a car a paint job or changing its engine would alter it. You can use property values of various objects in your scripts to set values elsewhere. Let's assume that a script in your project moves your car at a speed based on its horsepower. That line of script might look like the following:

 speedFactor = car.horsepower; 

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 the String object. For instance, the length of "Flash" is 5, because it contains five characters. In ActionScript, this would be written as follows:

 name = "Flash";  lengthOfName = name.length; 

The first line of code above 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, _rotation , and so on), not all object property names follow this convention. The reason for this is that the convention is a holdover from Flash 4 (when properties were introduced and ActionScript was much different than it is today).

NOTE

Objects can store arrays, variables, and even other objects all of which are considered properties.


Methods

A method represents a task that an object can perform. If you think of a VCR as an object, its methods include the abilities to play, record, stop, rewind, fast forward, pause, and so forth. A method consists of its name followed by a set of parenthesis. The methods of our VCR object would look like this:

 play();  rewind();  record(); 

To invoke a method of an object, you must first indicate the object (or the name of an instance of an object, which we'll explain in a moment), followed by a dot, then the name of the method:

 myVCR.record(); 

This tells the object named myVCR to start recording.

The parentheses included with method sometimes allow you to invoke it in a unique way using a parameter or set of parameter values. Using our VCR example again, let's say you wanted to record a TV show on Channel 8 from 10 p.m. to 11 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); 

As you can see, commas separate the different method parameters. Keep in mind that parameter values can be hard coded (as shown above), or they can be dynamic values such as variables. You can even use other methods as parameters.

Although many ActionScript objects have methods that accept parameters, not all do. Some simply perform tasks that don't require any special settings. For example, the stop() method of the MovieClip object simply causes the timeline to stop nothing more, nothing less.

Each object has a unique set of methods which makes sense because each object has a specific function.

graphics/04fig03.gif

Object methods in ActionScript perform all sorts of tasks, including the following:

  • Getting and setting values (see below)

  • 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 more throughout the book.

NOTE

An object does not have to contain any properties or methods to be considered an object. However, a object with no properties or methods is of no use.




Macromedia Flash MX ActionScripting Advanced. Training from the Source
Macromedia Flash MX ActionScripting: Advanced Training from the Source
ISBN: 0201770229
EAN: 2147483647
Year: 2002
Pages: 161

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