Functions as Methods of Objects


We've already discussed functions as procedure mechanisms in Flash movies. You can use functions to define a set of actions that are later executed when the function is invoked. In ActionScript, you can also use functions as methods of other objects. Methods are actions mapped to other objects. Unlike properties and values, methods carry out a task with that object. In this section, we deconstruct a Flash document file (.fla) that uses a function to create a menu completely from ActionScript.

On the CD-ROM 

Make a copy of the createMenu_100.fla file, located in the ch26 folder of this book's CD-ROM.

Open a local copy of the createMenu_100.fla file in Flash 8. You'll notice that the Main Timeline has a setup similar to the menuArray_100.fla file that we discussed in the last section. There are a series of labels, indicating sections of the Flash movie. Test this Flash movie, and you'll see a dynamic menu display. Clicking each button takes you to the corresponding section of the Flash movie.

Unlike our previous menuArray_100.fla example, however, notice that we have different text on the menu buttons than the text used in the frame labels. For example, the Our Products menu button takes you to the products label on the Main Timeline. For this Flash movie, a function with multiple arguments enables you to specify the text of the menu buttons separately from the targeted labels (and timelines).

Select the first frame of the functions layer. In the Actions panel, you'll see this function appear in the Script pane:

 function createMenu(aSections:Array):Void {    var sectionCount = aSections.length;    for(var i=0:Number; i < sectionCount; i++){       var section:Object = aSections[i];       var nDepth:Number = this.getNextHighestDepth();       var item:MovieClip = this.attachMovie("itemClip", "item_"+i, nDepth);       item.tLabel.text = section.btext;       item.targetClip = section.clip;       item.labelName = section.flabel;       item._y = i*45;    } } 

The createMenu() function has one argument: aSections. The value of this argument will be supplied via a method of a Movie Clip instance when the function is executed. Similar to our previous menuArray_100.fla example, an array is used to store the values of frame labels, button text, and target clips. In this way, you can create ActionScript that correctly uses frame labels in other goto actions, without worrying about the text that is actually used as a button item.

Select frame 1 of the actions layer, and view the code in the Actions panel. Some of the same actions from the previous section's example are re-used here. An empty MovieClip object named mcMenu is created, positioned, and assigned the createMenu() function as a method, just as duplicateMovieClip() or attachMovie() is a method of the MovieClip object:

 mcMenu.createMenu = createMenu; 

This line of code creates a new method called createMenu, specifically for the mcMenu instance on the Stage. It also sets this method to use the function createMenu as its value. Therefore, whenever you evoke the createMenu method of the menu object, the actions within the createMenu function will run.

Caution 

The act of creating and assigning a method name for an object does not actually execute the method or the function. We're simply defining a method for the object, so that it can be evoked later. Do not use parentheses for method (and function) assignment — doing so will execute the function upon assignment.

Note that you can use any method name you prefer — it need not match the name of the function as our example does. So, you could write:

 mcMenu.customMenu = createMenu; 

The function createMenu also uses the this syntax to make the function work in relation to the object that is executing it. this will correspond to mcMenu for the method assignment mcMenu.createMenu = createMenu. However, if we had another menu instance, such as mcMenu_2, that used the createMenu function as a method, this would refer to its path for its method. Herein lies the power of a function as a method of an object — you can assign the same function (and arguments) to several unique objects (or Movie Clip instances) on the Stage.

To execute the method createMenu for the menu instance, specify the method and any arguments you will supply to the method. In our example, the following line executes the createMenu() method for the mcMenu instance:

 mcMenu.createMenu(sectionNames); 

In this line of code, the sectionNames variable is passed as the argument to the createMenu function. The sectionNames variable is an array of Object objects. Each object contains three properties:

  • flabel: This property defines the frame label to be used in the menu item's gotoAndStop() action.

  • btext: This property defines the text to appear to the right of the button.

  • clip: This property declares which timeline should invoke the gotoAndStop() action.

In our example, three section objects are defined in the sectionNames array:

 var sectionNames:Array = [                     {flabel: "main", btext: "Home", clip: this},                     {flabel: "products", btext: "Our Products", clip: this},                     {flabel: "services", btext: "Our Services", clip: this}                    ]; 

When the createMenu() method is evoked, the createMenu() function parses the sectionNames array into the actions contained with the function. While you do not see the code structure of an array specified in the function, a local variable named section represents each object in the sectionNames array:

 var section:Object = aSections[i]; 

where aSections as the argument of the createMenu() function represents the passed value, sectionNames. In this more advanced usage of an array, each index of the array represents the section object with the three named properties. For example, when aSection[0] is evaluated in the for loop, the following object is returned:

 {flabel: "main", btext: "Home", clip: this} 

The section variable is used to take each object and apply its properties to dynamically created instances of the itemClip symbol:

 item.labelName = section.btext; item.targetClip = section.clip; item.targetFrame = section.flabel; 

The section.clip property is used to let each item instance know which timeline target it should address with its gotoAndStop() action (contained on the Button instance within the itemClip symbol in the Library). The section.flabel property assigns the proper frame label for the gotoAndStop() action for the Button instance, and the section.btext property assigns the text to the tLabel field within the item instance.

This movie also uses a clearMenu() function (and method) to delete the item instances.




Macromedia Flash 8 Bible
Macromedia Flash8 Bible
ISBN: 0471746762
EAN: 2147483647
Year: 2006
Pages: 395

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