In several of the preceding pages, you've seen examples of code surrounded by curly brackets (braces). These brackets indicate that all the code within them is to be executed as a group, and the statements within the curly brackets are called a code block. They are usually indented within the brackets for readability, a standard programming practice. When a control-movieclip is to carry out several statements when clicked, for example, those statements all go within curly brackets:

In that example, the code block consists of 3 statements which are carried out every time bee1control_mc is clicked.
Code blocks are also used within if/else statements if multiple statements are to be carried when a given condition is true or false. eg,
if (plane_mc._currentframe == 1) {
plane_mc.gotoAndPlay("flyaway");
flying = true;
} else {
plane_mc.gotoAndPlay("crashplane");
flying = false;
}
which tells Flash if the playhead of plane_mc is sitting on frame 1, send it to the frame labelled flyaway AND set the variable flying to true. Otherwise (if the playhead of plane_mc is on any other frame), send it to crashplane and set the variable flying to false.
Notice that checking for equality is done with two equal signs
(==).
correct: if (plane_mc._currentframe ==
1 ) { ... }
incorrect: if (plane_mc._currentframe =
1) { ... }
If you use just one (=) by mistake, Flash will try to do an assigment instead
of a comparison.
The generic form of the if/else statement is:

If you want to check for two or more things all being true before doing whatever is in the code block, use &&, eg:
if (plane_mc._currentframe==1 && bird._visible) {
// code block here
}
If you want to check whether either one thing or another is true before doing whatever is in the code block, use ||, eg:
if (balloon_mc._x > 500 || cloud_mc._x > 500) {
// code block here
}
Another common use of code blocks is when code is to be repeated for every element of an array. Here's an example where every element of the array songlist is added as a new line of a textfield named display_txt and also traced to the Output panel:
for (var i:Number=0; i<songlist.length; i++) {
display_txt.text += songlist[i] + "\n";
trace(songlist[i]);
}
and generically:

where the first statement is dimmed to show that it is not really part of the For loop structure, but that i should either be declared before the loop is done, or declared as part of the loop itself, as in the example code above.
If some condition is met and you wish to exit from the for loop before it iterates through all elements of the array, you can issue a break statement, eg:
var found:Boolean = false;
for (var i:Number=0; i<songlist.length; i++) {
// find out if "love" occurs in any song title
if (songlist[i].indexOf("love") > -1) {
found = true;
break;
}
}
if (found) {
trace(songlist[i]);
}
A side note: to break out of a nested for loop, you need to check for found in each level. Eg,:
var found:Boolean = false;
for (var i=0; i < 10; i++) {
for (var j=0; j < 10; j++) {
for (var k=0; k < 10; k++) {
if (i*100 + j*10 + k == 327) {
found = true;
break;
}
}
if (found) break;
}
if (found) break;
}
trace('i='+i+' j='+j+' k='+k);
Both of the code block examples above, if statements and for statements, show examples of code that needs to be grouped together for a specific purpose: in an if statement, the code block contains all the statements that are to be executed when a particular condition is met, and in a for loop, the code block contains all the statements that are to be executed repeatedly, for each of a number of conditions (eg, for i=0, then for i=1, etc). For the more general case, when you want to group statements together for any purpose, you can include them inside a function. A function is simply a placeholder in memory for a code block. We've looked at some of the functions built into Flash (and associated with particular class instances, particularly MovieClip), such as gotoAndPlay and stop.
You can also set up your own functions, as is done in the first example of this page, where several statements are surrounded by the words function() { ... } and assigned to the onRelease handler of a movieclip. The function in that case does not have a name -- it is set up once and immediately assigned to the onRelease event property of bee1control_mc and never used again. If instead we wanted to set up a function that we could reuse (say, for several different controls), it would be better to create the function separately, give it a name (which Flash will use as a pointer to its location in memory), and then later assign it to an event handler, which tells Flash to call that function whenever the event occurs (bee1control_mc is clicked).
Here is an example of setting up a function and then assigning it:
// here we define the function
function onBeeClick() {
bee1_mc.stop();
bee2_mc._visible = false;
bee1_mc.stopped = true;
}
// here we assign it to an event property:
bee1control_mc.onRelease = onBeeClick;
So, nowhere in that example do we actually call the function. We simply define it (it is a variable named onBeeClick of type Function) and then assign it (the variable onBeeClick, the name of the function) to the event property of bee1control_mc. What we are actually doing is making use of the fact that a Flash movie always listens for certain built-in events to occur (like clicks on a movieclip or button) and when one occurs, will respond to it by calling any function that has been assigned (in code) to handle that event.
You can also use functions to set up blocks of code which your movie will call itself when needed, instead of waiting for some event to occur. When the function is called, all of the statements in the function's code block will be executed. Here's the general format of a function definition and a function call, for a function which does something:

where "parameters" is a list of things that the function is expecting its caller to pass, and "arguments" is that same list of actual values passed by the caller. Here is an example. To try it, open a new Flash movie and put a dynamic textfield with instance name salutation_txt on stage. Paste this code into frame 1:
function showSalutation(lastName:String, gender:String):Void {
if (gender == "F") {
salutation_txt.text = "Ms. " + lastName;
} else {
salutation_txt.text = "Mr. " + lastName;
}
}
showSalutation("Smith", "F");
The function showSalutation is defined first. It has two parameters, lastName and gender. They are both of type String (that is, they are to be made up of any string of characters). The "Void" part indicates that the function will not return any value to the caller. The purpose of the function is to format the lastName that is passed in into a salutation string, based on the value of gender (also passed in), and display the result in a textfield named salutation_txt. One example of calling the function is shown, in which an argument of "Smith" is passed as the value of the lastName parameter, and "F" is passed as the value of the gender parameter. When the function is called, salutation_txt is filled with a salutation string based on those two arguments.
Notice that the arguments in the function call have to be in the order the function is expecting them. If you swap "F" and "Smith", you won't get the same result above (hmm, what will you get? try it and see if you can explain why you see what you see). If you leave off the second argument in a function call to a function expecting two parameters, the second will be read by the function as undefined. (If the function requires both parameters to execute properly, you can set it to check if the second is undefined and supply a default value. Otherwise, it will react as though any unpassed parameters have the value of undefined).
In the above example, we passed two arguments to a function and it filled in a textfield on stage with an appropriate string. Sometimes, instead of (or in addition to) the function actually doing something, you might just want it to do a calculation and return a value. Say, for example, your program needs to use a random number between two other numbers for some reason. Instead of having to type in code to find such a number every time you need it, you could set up a function that returns such a number based on two numbers you pass to it:
function randomBetween(a:Number, b:Number):Number {
return (a + Math.floor(Math.random()*(b-a+1)));
}
var someNumber:Number = randomBetween(50, 100);
trace(someNumber);
If you paste that code into frame 1 of an empty movie, every time you run it, you'll see a (probably different) random number between 50 and 100 in the output panel. That's nice, but not terribly useful. Here's a little variation in which we create a new function that will call our original function and show the result in the output panel. We can then assign that function to a movieclip's onRelease event property so that this will be done every time we click control_mc:
function randomBetween(a:Number, b:Number):Number {
return (a + Math.floor(Math.random()*(b-a+1)));
}
function getAndShow() {
var someNumber:Number = randomBetween(50, 100);
trace(someNumber);
}
control_mc.onRelease = getAndShow;
Let's make things a little more interesting and use the number returned from our randomBetween function to DO something, eg, make a movieclip randomly bigger or smaller every time we click on it:
function randomBetween(a:Number, b:Number):Number {
return (a + Math.floor(Math.random()*(b-a+1)));
}
function changeScaleRandom() {
var newscale:Number = randomBetween(0, 200);
this._xscale = newscale;
this._yscale = newscale;
}
thing_mc.onRelease = changeScaleRandom;
last update: 16 Feb 2006
Discussed on this page:
code block, if then else, for loop, syntax check, indent code, check equals, functions, event handler, assignment, randomBetween function
Other Resources
The examples of code blocks discussed on this page can also be described as providing Program Flow Control -- as shown in the section with that header in this Actionscript Reference Card.