Flash's built-in object classes (MovieClip, TextField, XML, and so on) come with a number of predefined methods that are useful in most situations. You may at times, however, need more functionality than they can provide. Using what you've learned thus far in this lesson, you can easily extend and enhance these methods to do just about anything imaginable. You can enhance existing methods in two ways, depending on the type of object you're working with. There are some objects you cannot create instances of: These are singular and universal to your project, and include the Mouse, Key, Selection, and Math objects. You cannot, for example, create an instance of the Mouse object it's universal, meaning you use the methods of the Mouse object to control the singular mouse that appears on screen. This is in contrast to object classes, for which you can create instances (MovieClip, TextField, and so on). When you cannot create an instance of an object, you can enhance a method of that object, using syntax that resembles the following: MathPlus = new Object(); MathPlus.__proto__ = Math; MathPlus.oldMax = Math.max; MathPlus.max = function(x, y, z){ firstComparison = MathPlus.oldMax(x, y) secondComparison = MathPlus.oldMax(firstComparison, z); return secondComparison; } Object.prototype.Math = MathPlus trace(Math.max(23, 56, 4)); This script will enhance the max() method of the Math object, which is normally used to find the higher of two numbers. For example, the following will set myVariable to a value of 456, the higher of the two numbers. myVariable = Math.max(456, 23); The above script enhances this method to find the highest of three numbers. Here's how: The first line creates a generic object named MathPlus . The next line uses the __proto__ property to make this object instance inherit the functionality of the Math object. NOTE We do not use MathPlus = new Math(); to establish inheritance in this scenario (as shown earlier when setting up inheritance between the Doctor and Person classes) because you cannot create instances of the Math object (thus, new Math() has no meaning). If we want the MathPlus object to inherit the functionality of the Math object, we must use the __proto__ property as shown which does basically the same thing but in a different way. (We discussed this property in Step 9 of the section "Creating a Subclass" earlier in this lesson.) At this point, MathPlus can do everything the Math object does. Thus, Math.round(34.7); and MathPlus.round(34.7); will do the same thing. The reason for doing it this way is that we can't directly manipulate the methods of the Math object (or any other object for which you can't create instances). Thus, to get around this, we create a proxy object that inherits the Math object's functionality (MathPlus ), change the functionality of the proxy object, and then (in a strange twist) tell ActionScript to think of the proxy object (MathPlus ) object as the new Math object. On the next two lines of script, two new methods are defined for the MathPlus object: oldMax , which is given the functionality of the original Math.max() method, and max , which is set up as the new and improved max() method. We needed to create oldMax because the new max() method still needs the original max() method's functionality. However, we needed to give it a new name because our improved method is now named max . The function that defines the new max() method is set up to accept three values (x , y , z ). The new method definition uses these parameter values in various ways to find the highest of the three numbers. In the line after the function definition that sets the new max() method's functionality, the new MathPlus object is assigned to the Math method of the Object class' prototype (meaning that the MathPlus object becomes the new Math object).
You can also enhance the Math object by extending the random() method to generate a random number between a range. To do that, you would use the following additional syntax (assuming MathPlus has been created and inheritance set as shown on Lines 1 and 2 of the previous example): MathPlus.oldRand = Math.random; MathPlus.random = function(min, max){ return MathPlus.floor(MathPlus.oldRand() * ((max - min) + 1)) + min; } You can see that this syntax is structured similarly to the previous example. Similar syntax is also used to extend methods of the Mouse, Key, and Selection Objects. Enhancing the methods of classes of objects, where instances can be created (for example, with MovieClip, TextField, and so on) is a bit different but not by much. In this exercise, we'll extend the duplicateMovieClip() method. Using this method, you can already specify which movie clip you want to duplicate, what the duplicate's name will be, and the depth at which the duplicate should be placed (for more information about this action, see Lesson 14, Controlling Movie Clips Dynamically). Now, we'll enhance it so that you can also move the original movie clip (the one that is duplicated) to specified x and y coordinates after the duplication process.
|