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 ObjectA 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:
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:
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 ObjectIf 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:
Again, methods are what make objects do things. With the Car object, you could have many methods to make the car do things:
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:
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 ObjectProperties 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:
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:
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. |