Functions as Methods of Objects Flash

We’ve already discussed functions as subroutine mechanisms in Flash movies. Functions can be used to define a set of actions that are later executed when the function is invoked. In Flash 5, you can use also functions as methods of other code objects. Methods are actions specific to actions. Unlike properties and values, methods carry out a task with that object. In this section, we deconstruct a .FLA file that uses a function to create a menu completely from ActionScript.

Open a local copy of the createMenu.fla file in Flash 5. You’ll notice that the Main Timeline has a setup similar to the arrayMenu.fla file that was discussed in the last section. We have a series of labels, indicating sections of the Flash movie. If you test this Flash movie, you’ll see a dynamic menu display. Clicking each button takes you to the corresponding section of the Flash movie.

Unlike our previous arrayMenu.fla example, though, 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).

Double-click the first frame of the functions layer. In the Actions Panel, you’ll see this function appear in the Actions list (note that the ¬ indicates a continuation of the same line of code; this character does not appear in the actual code):

The createMenu() function has three arguments: names, targets, and labels. The value for these arguments will be supplied as a method of a Movie Clip instance when the function is executed. Similar to our previous arrayMenu.fla example, we will use arrays to store the values of our frame labels (and section names). However, we’ll also create an argument (and array) to store the button text that will appear on the menu buttons. In this way, we can create ActionScript that correctly uses frame labels in other Go To actions, without worrying about the text that is actually used as a button item. The targets argument is used to create an array of timeline targets for each button item.

The function also uses the attachMovie method (instead of duplicateMovieClip) to use the menuItem Movie Clip symbol in the instance timeline executing the function. In our example, there is a menu Movie Clip instance on the Stage. This empty Movie Clip instance is assigned the createMenu() function as a method, just as duplicateMovieClip or attachMovie is a method of the Movie Clip object: = createMenu;

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

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: = 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 equal for the method assignment, = createMenu. However, if we had another menu instance, such as menu_2, that used the createMenu function as a method, then 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 unique objects (or Movie Clip instances) on the Stage.

To execute the method createMenu for the menu instance, you need to specify the method and any arguments you will supply the method. In our example, the following line (note that the ¬ indicates a continuation of the same line of code; this character does not appear in the actual code) executes the createMenu method for the menu instance:“Home,Our Products,Our ¬ Services”,”_root,_root,_root”,”main,products,services”);

In this line of code, the following arguments are passed to the createMenu function arguments:

When the createMenu method is evoked, the createMenu function parses this argument into the actions contained with the function. The split method (for String Objects) takes the values of names, targets, and labels, and makes each commadelimited item (item separated by a comma) into a separate array element:

While you will never see those lines of code in the function, you will see references to these array items. this.itemName[i-1] is used to take each itemName element and put it on the proper menuItem Movie Clip instance in the menu object. The itemTarget array is used to let the menuItem instances know to which timeline target to address the gotoAndStop() action (contained on the Button instance within the menuItem symbol in the Library). The frameLabel array assigns the proper frame label for the gotoAndStop() action for the Button instance. This movie also uses a resetMenu function (and method) to delete the menuItem instances and arrays.

Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Flash Topics