Understanding Object-Oriented Languages

     

ActionScript is an object-oriented programming (OOP) language. AS2 is even more strongly object-oriented than AS1. For approximately the last 40 years , OOP has been gaining ground as the programming paradigm of choice. In this chapter, I explain why and give some simple examples. Subsequent chapters go into more detail.

"Object-oriented" means "based on objects." An object is a data structure that enables you to do in programs what you do in everyday life: group complex phenomena under simple headings.

For instance, suppose I want my friend Joe to come to my party and bring his guitar. Both Joe and his guitar have many capabilities and characteristics, but I just call Joe and say, "Hey, are you free Saturday night? Great! Come to my party and bring your guitar!" I don't have to tell him how to come to my party or what his guitar is; Joe already knows those things.

Joe, in this case, is analogous to a programming object. I'm dealing with two of his properties: One property is a function, namely, coming to a party. The other property is an object: Joe's guitar. (Joe, in this analogy, is also an object. So you have an object as a property of an object.)

You can use objects in ActionScript to organize the complex behaviors and characteristics of your programs and give yourself simple interfaces to them. Like Joe, an object is something that you can refer to by a single name , even though it may have many properties and behaviors. By summarizing related functions and data properties under object names , you make your programs easier to work with and understand.

Every movie clip in Flash is an object. Thus, if you have even a little experience with Flash, the terminology of objects may be new, but objects themselves are not.

Let's look at the three basic building blocks of objects: primitive data, functions, and data structures. This discussion will constitute a whirlwind tour of the elements that make up an ActionScript program. Most of the remainder of this section is devoted to filling in the details.

The simplest form of data ”data primitives ”includes numbers and character strings. For instance, the number 6 is a primitive datum (piece of data), as is the string "Hello world!" . A third primitive data type is Boolean. A Boolean datum can have only one of two values, namely, true or false .

If you want more information about primitives, see the section in Chapter 20 on "The Nine ActionScript Datatypes," page 464 .


A function , as defined earlier in this chapter, is a named block of code embodying a procedure.

Primitive data and functions can be grouped into data structures. One type of data structure is an array , which is basically a numbered list, starting at zero. Thus, myArray[0] is the first element in the array named myArray . In an array containing the names of months, you would have myArray[0] = "January" .

If you want more information about arrays, see "Using the Core Classes," in Chapter 21, "Advanced ActionScript," page 556 .


Objects are another way of grouping data and functions into structures. Instead of accessing things via numbers, as you do in an array, you use names to access the items (called properties ) in an object.

The following are examples of some movie clip properties. They are all "read-write" properties: You can read them to determine the current state of the movie clip, or you can change them and thus control the movie clip.

  • _currentFrame ” A number; the movie clip's current frame on the Timeline.

  • _rotation ” A number; the number of degrees the movie clip has been rotated from its original position.

  • _visible ” A Boolean; controls the visibility of the movie clip.

  • _name ” A string; the name of the movie clip instance.

You access the elements of an array via the square bracket notation: myArray[0] . You typically access the properties of an object via "dot syntax," in which the name of the object and the name of the property are separated by a dot, as in these examples:

 myClip._currentFrame myClip._visible myClip._name myClip._rotation myClip.gotoAndPlay() 

In summary, objects are collections of properties. Each object has a name, and each property within an object has its own separate name. The property name refers either to a function or to data. The data may be a data primitive or a data structure such as an array or another object.

Data can also be stored in a variable , which is a container that is outside any data structure. For instance, the following statement stores the number 10 in a variable named x :

 x = 10; 

Functions in Objects Are Methods

Both arrays and objects can contain functions. However, object properties that refer to or contain functions are called methods . A function that is an element of an array is still just called a function .

Methods come under the grab-bag term actions , which also include global functions such as eval that are not associated with any object, statements such as break that control program flow, and directives such as include that give instructions to the ActionScript compiler. Methods is a more precise term.




Using Macromedia Studio MX 2004
Special Edition Using Macromedia Studio MX 2004
ISBN: 0789730421
EAN: 2147483647
Year: N/A
Pages: 339

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