Recipe 1.13. Creating Reusable Code


Problem

You want to perform a series of actions at various times without duplicating code unnecessarily throughout your movie.

Solution

Create a method and then call (i.e., invoke) it by name whenever you need to execute those actions. When a function is a member of a class, it is often called a method.

Here is how to create a method of a class:

                accessModifier function functionName (  ):ReturnDataType {   // Statements go here. }

To call (i.e., execute) the named method, refer to it by name, such as:

                  functionName(  );

Discussion

Grouping statements into a method allows you to define the method once but execute it as many times as you'd like. This is useful when you need to perform similar actions at various times without duplicating the same code in multiple places. Keeping your code centralized in methods makes it easier to understand (because you can write the method once and then ignore the details when using it) and easier to maintain (because you can make changes in one place rather than in multiple places).

Like class variables, methods can be declared with access modifiers. These determine which other classes are able to call the methods. The available access modifiers are:


private

Can only be accessed from within the class itself.


protected

Can be accessed by the class or any subclass. This is instance-based. In other words, an instance of a class can access its own protected members or those of its superclasses. It cannot access protected members on other instances of the same class.


internal

Can be accessed by the class or any class within the same package.


public

Can be accessed by any class.

The definition of private has changed since ActionScript 2.0, where it allowed access by subclasses. If you do not specify an access modifier explicitly, the method takes on the default internal access.

The following class defines a drawLine method and calls it 10 times, rather than repeating the three lines of drawing code for each line:

package {     import flash.display.Sprite;     public class ExampleApplication extends Sprite     {         public function ExampleApplication(  ) {             for(var i:int=0;i<10;i++) {                 drawLine(  );             }         }              private function drawLine(  ):void {             graphics.lineStyle(1, Math.random(  ) * 0xffffff, 1);             graphics.moveTo(Math.random(  ) * 400, Math.random(  ) * 400);             graphics.lineTo(Math.random(  ) * 400, Math.random(  ) * 400);         }     } }

Another important method type is a static method. Static methods aren't available as a member of an instance of that class, but instead are called directly from the class itself. For example, in a class named ExampleApplication, you could define a static method as follows:

public static function showMessage( ):void {     trace("Hello world"); }

You could then call that method like so:

ExampleApplication.showMessage(  );

Some classes contain nothing but static methods. The Math class is an example. Note that you don't have to create a new Math object to use its methods; you simply call the methods as properties of the class itself, such as Math.random( ), Math.round( ), and so on.




ActionScript 3. 0 Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2007
Pages: 351

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