Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

macromedia flash mx - Reinhardt R.

Reinhardt R., Lott J macromedia flash mx - John Wiley & Sons, 2004. - 987 p.
ISBN 0-7645-4354-7
Download (direct link): macromediaflash2004.pdf
Previous << 1 .. 87 88 89 90 91 92 < 93 > 94 95 96 97 98 99 .. 419 >> Next

class Car {
// Other private member declarations here...
private var _driveIntervalID:Number = null;
// Constructor and other getter/setter methods here ...
// The drive() method should accept a Boolean parameter. public function drive(bStartDrive:Boolean):Void {
// If the parameter value is... if(bStartDrive) {
// ... true, tell Flash to start invoking the // incrementMileage() method of this class at a // rate of approximately once every second. Assign // the interval ID to _driveIntervalID.
_driveIntervalID = setInterval(this,
"incrementMileage",
1000);
}
else {
// ... otherwise, clear the interval. clearInterval(_driveIntervalID);
}
}
// The incrementMileage() method is a private method // that simply increments the value of _mileage by 1. private function incrementMileage():Void {
_mileage += 1;
}
}
13. In the FLA file, create a new Movie Clip symbol named DriveCar. In the symbol, draw a filled circle.
14. On the main timeline, rename the default layer to Actions, and create a new layer named Artwork.
15. On the Artwork layer, drag an instance of the DriveCar Movie Clip symbol. Name the object mcDriveCar.
172 Part II ¦ Laying the ActionScript Foundation
16. Select the Actions layer, and open the Actions panel. Modify the code so that it reads as follows:
var crTest:Car = new Car("Oldsmobile", "Alero", "blue");
// Create a custom property for the mcDriveCar instance.
// The property references the Car object, crTest. This // enables you to reference the Car object within the // MovieClip object's event handler methods. mcDriveCar.carObj = crTest;
// Define an onPress() event handler method. When the user // clicks on the MovieClip, Flash tells the Car object to // invoke the drive() method, and it passes it a value of // true.
mcDriveCar.onPress = function():Void { this.carObj.drive(true);
};
// Define an onRelease() event handler method. When the // user releases the click on the MovieClip, Flash tells // the Car object to stop driving by invoking the drive()
// method with a value of false. Also, use the trace()
// action to output the description property value. mcDriveCar.onRelease = function():Void { this.carObj.drive(false); trace(this.carObj.description);
};
17. Test the movie. Click and hold the circle for a few seconds. Then, when you release the click, the current description should display in the Output panel. Do this a few times. Notice that the mileage keeps increasing cumulatively.
In this exercise, when the user clicks the circle, the Car object’s drive() method is invoked, telling the object to start driving. Internally, that method sets an interval by which the private method incrementMileage() i s invoked once per second. This interval continues as long as the user holds down the mouse click. As soon as the click is released, the drive() method is again invoked — this time telling the Car object to stop driving. This causes the interval to be cleared, so the mileage is no longer incremented. However, the value for the mileage is not reset. Thus, the next time the user clicks the circle, the mileage increases even more.
Working with Advanced Class Topics
Now you have learned all the basics needed to create simple classes. The next step is to examine some of the more advanced topics. These topics include working with packages (organizing your classes), extending classes (creating parent/child relationships between classes), creating and implementing interfaces (rules for how to create a class), and making dynamic classes.
Chapter 7 ¦ Programming with Objects and Classes 173
Organizing Your Classes with Packages
It is a good idea to organize your classes. This is true for several reasons. First of all, and likely rather apparently, organizing your classes helps you to locate classes and to remember what their purposes are. In addition, it is likely that you will download and install custom classes that were designed by others. It is possible, therefore, for you to end up with classes with the same name. If all your classes simply go in one directory, you run into a problem trying to have classes with the same name.
You organize your classes the same way that you would organize other files on your computer — using directories. In object-oriented terminology, these organizational directories are called packages. These directories (and their subdirectories, if applicable) should be placed somewhere within the classpath.
Consider the following scenario: You have created three classes — Rabbit, Hummingbird, and Ladybug. Now, these classes all happen to be related because they are animals. It makes sense to then package them together into an animal package. You can accomplish this by doing two things:
¦ First, you need to make a slight modification to the code in the AS file. The name of the class should reflect the package. Packages are indicated using dot syntax. For example, the Rabbit class should be declared as follows:
class animal.Rabbit {
// Class code goes here.
Previous << 1 .. 87 88 89 90 91 92 < 93 > 94 95 96 97 98 99 .. 419 >> Next