Object-Oriented Programming


Object-oriented programming is more than a type of programming language; it is how you approach a problem in the context of programming.

Back in the early days of programming, everything ran in a straight line with hundreds of conditionals to check to see if something should happen or not. For instance, let's say driving is the program; the program would start by going to the car, opening the door, putting the key in the ignition, and turning the key (which would start the car). It then would go through a series of conditional statements checking to see if the user wants their headlights on or not, if they want the radio on or not, and if the emergency brake is on or not (see Figure 8.1). Already this program is long and painstakingly slow, and we haven't even begun to drive yet.

Figure 8.1. The old way of programming.


This is where object-oriented programming can come in handy. Instead of having all of these steps and conditional checks, you could have a single object on which all the code could be based.

What Is an Object?

An object, as it relates to programming, is a self-contained chunk of code containing the three very important elements that all objects contain: properties, methods, and events.

These three elements of objects are what makes them so powerful and gives them the ability to be self-contained.

Properties of an Object

A property of an object is raw data. Basically, each property of an object holds information about that specific instance of an object. Some properties can be read and changed, but others are read-only.

Here are some examples of built-in object properties in Flash:

  • movieClip._x The horizontal position of a movie clip object, read and write editable.

  • array.length A read-only property of the Array object that holds the number of elements in a given array.

  • textField.text A read-write property for getting and setting the text of a text field object.

  • Math.PI A constant property of the Math object, which is read-only; it translates to 3.14159265358979 roughly.

  • textformat.bold A read-write property of the TextFormat object, and can be set to either true or false.

These are just a few of the many properties already built into Flash and its objects.

Going back to the driving program, if we were to build an object for it, the object would be a Car object.

Some of its properties might be the following:

  • Car.type The type of car being driven; Mustang, Liberty, or 911 Carerra Twin Turbo.

  • Car.color The exterior color of the car; red, blue, or green.

  • Car.speed This property would constantly change depending on how fast the car is being driven.

  • Car.passengers This property could be either the number of passengers in a car or an array with the names of each passenger in the car.

Of course, the Car object could have literally hundreds more properties.

But that's just the information about the car. What about what the car does?

Methods of an Object

If properties are the "adjectives" of objects, then methods are the "verbs." Methods make things happen. A method, when it comes down to it, is nothing more than a function that is associated with a specific object.

Here are a few built-in methods for objects in Flash:

  • movieClip.stop() This method stops the play head for a specific movie clip's timeline.

  • array.reverse() This method reverses the order of elements in a given array.

  • textField.getDepth() This method returns the depth number of a specific text field.

  • Math.random() This method returns a random number between 0 and 1.

  • color.getRGB() This method returns information on a Color object's color.

Again, methods are what make objects do things.

With the Car object, you could have many methods to make the car do things:

  • Car.start() This method would start the car.

  • Car.brake() This method would apply the brakes.

  • Car.honkHorn() This method would let the person in front of you know you don't appreciate them cutting you off.

  • Car.accelerate() This method would accelerate the car.

Of course, these methods do a specific thing, no variance whatsoever. When the Car.start() method is called, the car starts; when the Car.honkHorn() method is called, the car honks. But that is not the limit of methods: They can have parameters in which you pass information to them that can dictate what will happen and to what degree.

For instance, the movie clip object has a method called the gotoAndStop() method. By itself, it can do nothing, but if you pass it information, it can run that specific task.

Here are some examples of this method with parameters:

  • movieClip.gotoAndStop(2) This method will take the play head to frame 2 and stop.

  • movieClip.gotoAndStop("end") This method will take the play head to the frame labeled "end" and stop.

With this in mind, we could make our methods for the Car object more reusable. For instance, we could make the accelerate() method accelerate to a certain speed like this:

 Car.accelerate(55); 

Now the car will accelerate to 55 miles per hour and hold that speed.

So now you see the power of methods, the code that makes things work. The last element that objects are made of is events.

Events of an Object

Properties are information about objects. Methods do things with objects. Events tell Flash when certain things have occurred with objects. This is the most difficult aspect of objects to understand, which is why a whole chapter is devoted to events (Chapter 14, "Events"). Events usually have code associated with them that will function when triggered through either a callback or a listener. This makes it easy to call code at only certain points.

For example, here are some built-in events of Flash objects:

  • movieClip.onEnterFrame This event fires as close to the frame rate of a given movie clip as possible, for instance, if the frame rate is 12 frames per second (the default setting), the event will fire approximately 12 times per second.

  • textField.onChanged This event fires when a user changes the content in a specific text field.

  • button.onRelease This event fires when a user releases a button after it has been clicked.

  • XML.onLoad This event is triggered when an XML document has completely loaded into Flash.

  • click This is an event for the Button component.

You can see why these events are very important to objects. If events did not exist, you would have to use looping conditional statements to check to see if something has occurred, but with events, you can have code ready whenever the event fires.

Here is an example of loading content into Flash and checking to see if it has loaded without events:

 //there is a property coming in called "done" var sample:LoadVars = new LoadVars(); //load the file in sample.load("test.txt"); //use a loop to see if "done" has been received this.onEnterFrame = function(){      if(sample.done == "true"){           //do something           //get rid of the loop           delete this.onEnterFrame;      }else{           //continue to wait      } } 

The preceding block of code uses a movie clip event to continuously check to see if the variable done has been loaded into the LoadVars object we created. If it has been loaded, the code executes on the information, placing it either in a component or a text field, and then it destroys the looping statement.

Now here is the code with the onLoad event:

 //create the LoadVars object var sample:LoadVars = new LoadVars(); //the event for the LoadVars object sample.onLoad = function(){      //do something } //load the file in sample.load("test.txt"); 

This block of code, besides being much shorter, is more efficient. Instead of having a looping statement look to see if the content has loaded, an event triggers and lets Flash know when the content is loaded.

Now that you see the benefit of events, let's take that approach back to the Car object. Although it's hard to imagine because we take them for granted, a car has a great many events. Here are a few of them:

  • Car.onStart Lets Flash know when the car starts.

  • Car.onDoorOpen This event triggers when any of the doors open.

  • Car.onBrake Lets Flash know the brake has been applied so that it knows to turn on the brake lights.

  • Car.onCrash This event could trigger the air bag to be released and notify other "emergency objects" to come see if everyone is okay.

Events have a high priority when writing object-oriented code.

Now that we've discussed the three major features of objects, we're going to learn how objects are created.




Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319

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