<classwork>

Functions

Functions are terrific. You can store pretty much all of your programming in them and then call them later when needed. Many programmers put all of their Flash programming on Frame 1 in a Frame action with Functions. Then when they need them, such as pushing a button, they are called. It is so convenient because unlike putting programming all over your Flash file (on and in Movie Clips, on Buttons, in Scenes) you can have everything in one place to easily check, fix, print out, etc.

After a function is defined, it can be called from any Timeline, including the Timeline of a loaded SWF file.

Defining a function

Functions, like variables, are attached to the Timeline of the movie clip that defines them, and you must use a target path to call them.

function gtp () {
_root.gotoAndPlay("start");
}

As with variables, you can use the _global identifier to declare a global function that is available to all Timelines without using a target path. To define a global function, precede the function name with the identifier _global, as shown in the following example:

_global.function gts () {
_root.gotoAndStop("goodspot");
}

Now anywhere you call this function, you will go to the label named "goodspot" on the main scene timeline.

To define a Timeline function, use the function action followed by the name of the function, any parameters to be passed to the function, and the ActionScript statements that indicate what the function does.

The following example is a function named gtp2 with the parameter framelabel. "gtp" is my made up function name that stands for gotoAndPlay. Framelabel is the parameter that can be altered, changing the functions instructions.

function gtp (framelabel) {
_root.gotoAndPlay(framelabel);
}

Pass the Parameters

Parameters allow for fluidity. You can make a function that is very flexible.

Here's a better example:

The Function
function fillOutScorecard(initials, finalScore) {
scorecard.display = initials;
scorecard.score = finalScore;
}

The Call for the Function
fillOutScorecard("LEB", 45000);

When the function is called, the required parameters must be passed to the function. The function substitutes the passed values for the parameters in the function definition. In this example, scorecard is the instance name of a movie clip; display and score are input text fields in the instance. The following function call assigns the value "LEB" to the variable display and the value 45000 to the variable score.

LEB then sees their name in display text field and their score in the score text field


download the functions sample file(file name: functions.fla)
right click on the file (control-click for Mac) and select to download to your drive.


Functions with Buttons & Movie Clips

An event handler method is a class method that is invoked when an event occurs on an instance of that class. For example, the Button class defines an onPress event handler that is invoked whenever the mouse is pressed on a Button object. Unlike other methods of a class, however, you don't invoke an event handler directly; Flash Player invokes it automatically when the appropriate event occurs.

By default, event handler methods are undefined: when a particular event occurs, its corresponding event handler is invoked, but your application doesn't respond further to the event. To have your application respond to the event, you define a function with the function statement and then assign that function to the appropriate event handler. The function you assign to the event handler is then automatically invoked whenever the event occurs.

Button Function

For example, suppose you have a button named next_btn on the Stage. The following code assigns a function to the button's onPress event handler; this function advances the playhead to the next frame in the Timeline.

next_btn.onPress = function (){
nextFrame();
}

In the above code, the nextFrame() function is assigned directly to onPress. You can also assign a function reference (name) to an event handler method and then define the function later. The button instance name is "next_btn".


next_btn.onPress = goNextFrame;


function goNextFrame() {
nextFrame();
}

download the button functions sample file(file name: buttonfunctions.fla)
right click on the file (control-click for Mac) and select to download to your drive.

 

Movie Clip Functions

These behave just like button functions. Just define them and then make sure to use the instance name of the movieClip in front of the handler and function instructions.


Event Handler Methods

METHOD ACTIONS EQUIVALENT USE WITH MOVIECLIPS USE WITH BUTTONS
onPress on (press) yes yes
onRelease on (release) yes yes
onReleaseOutside on (releaseOutside) yes yes
onRollOver on (rollOver) yes yes
onDragOver on (dragOver) yes yes
onDragOut on (dragOut) yes yes
onKeyDown on (keyPress)/onClipEvent (load) yes yes
onKeyUp onClipEvent (load) yes yes
onSetFocus none yes no
onKillFocus none yes no
OnLoad onClipEvent (load) yes no
OnUnload OnClipEvent (unload) yes no
OnEnterFrame OnClipEvent (enterFrame) yes no
OnMouseDown OnClipEvent (mouseDown) yes no
OnMouseUp OnClipEvent (mouseUp) yes no
OnMouseMove OnClipEvent (mouseMove) yes no