ENHANCING EXISTING OBJECT METHODS


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).

graphics/06fig19.gif

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.

  1. Open objectProject2.fla in the Lesson06/Assets folder.

    Since we worked on this project in the previous exercise, you should be familiar with the elements it contains. In this exercise, we'll place scripts on Frame 1 as well as on the double-square buttons on the top-left part of the screen.

  2. With the Actions panel open, select Frame 1 and add the following script:

     MovieClip.prototype.oldDuplicateMovieClip =  MovieClip.prototype.duplicateMovieClip;  MovieClip.prototype.duplicateMovieClip = function(name, depth, moveX,  moveY){    this.oldDuplicateMovieClip(name, depth);    this._x = moveX;    this._y = moveY;  } 

    We've placed this script on Frame 1 so that it will execute as soon as the movie begins to play and the duplicateMovieClip() method will be enhanced and available for use by movie clip instances from that point forward.

    While we couldn't directly manipulate the methods of the Math object (which require a proxy object, as discussed), we can directly manipulate the methods of the MovieClip class because these methods exist on the MovieClip.prototype object, which is easy to change.

    The first line of the script places all of the functionality of the original duplicateMovieClip() method into oldDuplicateMovieClip . We did this because we need to retain its functionality when defining how the enhanced version of the method should work. The next line begins the function that defines the new and improved duplicateMovieClip() method. This method is set up to accept two additional parameters: moveX and moveY (which we'll explain how to use in a moment). The first line of the function uses the oldDuplicateMovieClip() method to perform the task of the original duplicateMovieclip() method that is, duplicate a movie clip, give it a name, and assign it a depth. The use of this in the function definition references the original movie clip instance (the one being duplicated). The next two lines use the moveX and moveY parameter values to move the original instance the specified amounts. Our new duplicateMovieClip() method is ready for use by all movie clip instances!

  3. With the Actions panel open, select the smaller of the double-square buttons and attach this script:

     on(release){    smallHead.duplicateMovieClip("myHead1", 10, 200, 150);  } 

    graphics/06fig20.gif

    When the button is released the smallHead instance is duplicated. The duplicate is given a name of myHead and a depth of 10. The last two values represent the values for moveX and moveY , respectively, which are used to move the original instance after the duplication process. Thus, smallHead is moved to an x coordinate of 200 and a y coordinate of 150.

  4. With the Actions panel still open, select the larger of the double-square buttons and attach this script:

     on(release){    bigHead.duplicateMovieClip("myHead2", 20, 100, 200);  } 

    This will duplicate the bigHead instance in the same way as described in the previous step.

  5. Choose Control > Test Movie to test the project up to this point.

    Pressing either of the buttons we just scripted will duplicate the specified instance, then move the instance to the specified x and y coordinates.

    This is a simple example of how you can enhance an existing method; however, you can extend them in complex ways as well and in the process make use of any number of other ActionScript actions. You can extend methods for other classes (for example, TextField, Sound, Color, XML, and so on) in the same way as shown in Step 2 (that is, by changing the functions on their prototype objects).

  6. Close the test movie and save your work as objectProject3.fla.

    This completes this exercise. We'll continue to build on this project in the following exercise.



Macromedia Flash MX ActionScripting Advanced. Training from the Source
Macromedia Flash MX ActionScripting: Advanced Training from the Source
ISBN: 0201770229
EAN: 2147483647
Year: 2002
Pages: 161

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