Using Functions and Conditional Statements

Functions are blocks of code that are typically reused many times in a FLA file. Functions can return a value when they're done doing what they do and have parameters passed to them to modify how they execute. Parameters (sometimes called arguments) allow you to pass an unchanging (or static) value or variable into a function. Then the value(s) can be used and manipulated in your function's code. Parameters, then, change how your function executes.

When you're cooking dinner at home, you're executing a function. It's a function because you weren't born knowing how to cook. You had to be taught by your father or grandmother or Julia Childs (how we miss her). The parameters that modify your cooking function might be what, when, how long. If you were to write your cooking function, it would probably look like the example below:

cookYummyFood=function(what,when,howLong) {      you.prepare(what);      placeInOven(howLong);      cookIt=when      return what }

In this example, cookYummyFood is the name of the function. What is the dish you are going to prepare, when is what time you're going to put it together, and howLong is the amount of time it will be in the oven. When you call the function, it would look like the example below:

Self.cookYummyFood(chicken, 5:00,1hr);

When it's finished executing, it returns the chicken. That is to say, the chicken is removed from the oven. Mmm...chicken.


You might notice that components also have parameters. This means that the parameter values that you enter or set using the Property inspector or Component inspector panel are setting particular values within the ActionScript used by the components. ActionScript in each component recognizes what to do, based on the values you set in the Flash authoring environment.

Functions are used all throughout Flash and ActionScript. Many are built in, but you can actually make your own to suit your specific needs. You've already seen several examples of functionsfrom the simple stop to the onLoad method of the LoadVars class. When you find yourself writing the same scripts over and over, changing similar code blocks into a single function with parameters that stand for the few things changing helps you manage your ActionScript code.

A conditional statement is different from a function and executes if a particular condition evaluates to TRue. A condition evaluates to a Boolean: either true or false. Depending on what value is the outcome determines whether code is executed or not, or sometimes what code is executed. In the following example a conditional statement is used to determine if dark chocolate is good for your health. If the doctor says it is, then you can eat it. Otherwise, you're out of luck:

if (darkChocolate == healthy) {  //if this statement is true, do this; } else {  //if dark chocolate isn't healthy, do this      self.weep(); }

So you see, you use conditional logic in your everyday life, when you're at the candy counter trying to choose between dark chocolate and something good for you (blech). In ActionScript, as in life, you have to evaluate, then execute.


The double equals seen in the previous code means you are comparing two values. Essentially, you are seeing if one thing equals something else. Think of it as matching.


As you have seen more than a few times by now, code typically uses varying amounts of indentation in it. This isn't done for any other purpose than to make your code easier to read. Indented code helps you debug, sometimes because it's easier to discover whether you have forgotten to close a bracket. Indentation also helps you clearly separate your statements from the conditionals. You can use the Auto Format button found in the Actions panel toolbar to add proper indentation to your ActionScript.

Macromedia Flash 8. Training from the Source
Macromedia Flash 8: Training from the Source
ISBN: 0321336291
EAN: 2147483647
Year: 2004
Pages: 230
Authors: James English © 2008-2017.
If you may any questions please contact us: