Overview of Functions as Subroutines Flash

A primary building block of any scripting or programming language is a subroutine. A subroutine is any set of code that you wish to reserve for a specific task. A subroutine is useful for code that you wish to reuse in multiple event handlers (for example, Button instances and keyframes). In Flash ActionScript, subroutines are called functions, and are created with the function action.

What functions do
A function (or subroutine) sets aside a block of code that can be executed with just one line of code. Functions can execute several actions, and pass options (called arguments) to those actions. All functions must have a unique name, so that we know what to reference in later lines of code. In a way, functions are equivalent to your own custom additions to the Flash ActionScript language. In Flash 4, we used the call action to execute code blocks located on other keyframes in the Flash movie. In Flash 5, we can define a function on a specific timeline, and refer to its path and name to execute it.

When to create a function
For people new to scripting, perhaps the most confusing aspect of functions is knowing when to create them in a Flash movie. Use the following guidelines to help you determine when a function should be created:

  • If you find yourself reusing the same lines of code on several Button instances, Movie Clip Objects, or keyframes, then you should consider moving the actions to a function. In general, you should not pile up ActionScript on any single Button instance or keyframe.
  • If you need to perform the same operation throughout a Flash movie, such as hiding specific Movie Clip instances on the Stage, then you should consider defining a function to take care of the work for you.
  • When you need to perform complex mathematical operations to determine a value, then you should move the operations to a function.

How to define a function
When you add a function to a keyframe on the Main Timeline or a Movie Clip timeline, you are defining the function. All functions have a target path, just like other Objects in Flash. All functions need a name followed by opening and closing parentheses, but arguments (options to pass to the function) inside the parentheses are optional.

As a simple example, let’s say you wanted to create a function that has one goto AndStop() action. This function will have a shorter name than gotoAndStop(), and will be faster to type and use in our ActionScript code. We’ll place this on the first keyframe of our Main Timeline.

This function, when evoked, will send the Main Timeline playhead to the start label We could further expand the functionality of gts() by adding an argument, which we’ll call frameLabel:

In this version of the gts() function, instead of hard-coding a frame label such as start into the actual gotoAndStop() action, we specify an argument with the name frameLabel. Just like variable names, the names of your function and its arguments are entirely up to you—the name frameLabel has no significance. Flash simply knows that if we pass an argument to the gts() function, that it should place that argument where the frameLabel term occurs in our actions. An argument acts as a placeholder for information that will be supplied to the function on a per-use basis; that is, we can specify a different value for frameLabel each time we evoke the gts() function.

How to execute a function
After you have defined a function on a timeline’s keyframe, you can create actions that refer to the function’s actions. The standard method for executing a function is:

[path to function].functionName(arguments);

At the end of the previous section, we defined a function named gts() on the Main Timeline. If we added a Button instance to our movie, we could then execute the function from the Button instance with the following code:

When this Button instance is clicked, then the function gts() on the _root timeline (the Main Timeline) is executed, and passed the argument “start”. In our function gts(), we defined frameLabel as an argument that occurs in the gotoAndStop() action. Therefore, the Main Timeline will go to and stop on the “start” frame label. Later , we use functions to create a dynamic reusable menu system in a Flash movie.



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

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

Flash Topics