Flash Functions and Callbacks
syllabus | schedule | assignments | Web Resources | ARTC courses
[Flash Intro]     [Drawing]     [Other Tools]     [Modify/Import]     [Symbols]     [Animation]     [Navigation]     [Publishing]    [Audio]     [ActionScript]     [Textboxes]     [Conditionals/Loops]     [Functions]     [Timing]     [Interaction]    [KeyPressed]    
Functions and Callbacks

• Functions are containers for sets of actions (instructions), just like a variable is a container for an individual piece of data. Functions are re-usable and can help in breaking large chunks of code into smaller, more manageable pieces.

• Functions also make updating or changing code simpler because you only have to make changes in one spot of the actionscript instead of multiple places.

-Ex. 1 of declaring a function in the old style:

function doSomeMath() {

myNumber += 5; //adds 5 to var value.
myNumber *= 2; //doubles var value.

}

-Ex. 2 of declaring a function as a callback:

doSomeMath = function() { //this line is the only thing different.

myNumber += 5; //adds 5 to var value.
myNumber *= 2; //doubles var value.

}

• What is the difference between the old style and the callback?

-Ex. 1 is a function named doSomeMath and is the style most used in Flash 5.

-Ex. 2 is an anonymous function because it doesn't have a name. Instead, the = sign is used to store the nameless function in a container called doSomeMath.

-The function in Ex. 1 has to exist on the current timeline, the callback function can exist anywhere in the movie, not just the current timeline, making it more flexible and readily available.

-Ex. of calling a function:

if (if this is true) { //conditional statement tests to see if actions will be executed.

myNumber = 4; //input value.
doSomeMath(); //call function to update myNumber value.
trace(myNumber); //used to see results in output window.

}

//creates a callback referring to a button instance named: red_btn
//and executes the code, including the function, only on release of the button.

red_btn.onRelease = function() {

myNumber = 4; //input value.
doSomeMath(); //call function to update myNum value.
trace(myNumber); //used to see results in output window.

}

-Ex. of simplifing code:

walkToStore = function(){ //walkToStore handles all actions related to its name.

do this;
do that;
etc;

}

goShopping = function(){ //goShopping is defined into subtasks, which are other callbacks.

walkToStore();
findItems();
payForItems();
walkHome();

}

for(i=0; i<10; i++){

goShopping(); //is a callback used to simplify the for loop. It is one large task.

}

• A function of either style must be declared before being called. All the subtasks would be defined first, then goShopping is declared, then goShopping can be called in the loop.

-Ex. of using an argument within a function:

typeOfItem = "bananas"; //typeOfItem is a variable that will be used as an argument.

findItems = function(typeOfItem){

switch (typeOfItem){ //uses argument to choose an action to run.

//each action updates the variable: quantity.
// In this case, typeOfItem = bananas; therefore quantit = 5.

case "bananas": quantity = 5; break;
case "apples": quantity = 3; break;
case "oranges": quantity = 2; break;
default: quantity = 0; break;

}

}

goShopping = function(typeOfItem){ //goShopping is defined into subtasks, which are other callbacks.

walkToStore();
findItems(typeOfItem);
//uses argument to specify which item.
payForItems();
walkHome();

}

for(i=0; i<10; i++){

goShopping(typeOfItem); //a callback with the argument: typeOfItem.

}

-Ex. of returning a value from a function:

doSomeMath = function(myNumber) {

var myNumber;
myNumber += 5; //adds 5 to var value.

myNumber *= 2; //doubles var value.
return myNumber;

}

x = 4;
y = 5;

x = doSomeMath(x);
trace(x);
//answer: 18.

y = doSomeMath(y);
trace(y);
//answer: 20.

y = doSomeMath(x);
trace(y);
//answer: 46.

Nested functions are functions within functions.

-Ex:

double = function(x) {

var x;
x =+ x;
return x;

}

doSomeMath = function(x) {

var x;
x = double(x);

//or x = double(double(x)); instead of calling it on two lines.

x = double(x);
return x;

}

Functions with multiple arguments.

-Ex:

function division(x, y) { //receives to variables (x and y) as the arguments.

var x, y;
return x / y;

}

To call this function:

x = 30;
y = 6;
z = division(x, y);
//z should = 5