Using Parameters in Functions


So far you have seen how to create functions and place scripts inside them. This is not very dynamic, though, in that after the information is in the function, it cannot be adjusted for different situations. Now we'll use parameters to change this limitation.

Parameters in a function act similarly to variables in ActionScript. They can be changed on-the-fly whenever the need arises. You place the parameters in parentheses following the name of the function, which looks something like this:

 function myFunction(parameters:ParameterType):ReturnType{     //script involving parameter; } 

You can also optionally set the type for the parameters, which is very helpful in debugging, as you will see in the next example.

The preceding code is simply a generic template, so let's look at some examples. The first example runs a simple TRace statement:

 //create a function with a string parameter function myTrace(name:String):Void{     trace(name); } //now we will run the function twice with two different parameters myTrace("Eddie"); myTrace("Lou"); //output: Eddie //        Lou 

You can see we used the String data type to make sure that is the type of data being passed into the parameters. Notice that if you change one of the parameters to the number 15, you will receive this error in the Output panel when you test the movie or check your code:

 **Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 7: Type mismatch.      myTrace(15); 

The preceding example is a basic example of using parameters with functions, so let's return to the previous fading example and see how to use parameters with conditional statements.

As before, you will want to set a point that your movie clip will fade to, but this time you'll use three parametersthe instance name of the movie clip, the point to fade the movie to, and the amount to fade the movie by. In this example, you'll use a loop statement instead of a conditional statement:

 function fade(movie:MovieClip, fadePoint:Number, amount:Number):Void{      while(movie._alpha > fadePoint){         movie._alpha -= amount;     } } //now the function is made, let's run it on a couple movies fade(myMovie,50,5); function fade(movie:MovieClip, fadePoint:Number, amount:Number):Void{     movie.onEnterFrame = function(){          if(movie._alpha > fadePoint){             movie._alpha -= amount;         }     } } //now the function is made, let's run it on a couple movies fade(myMovie_mc,50,5); fade(myMovie2_mc,20,2); fade(myMovie2,20,2); 

The preceding code would be placed in the timeline where both movies reside. Also note that because we are using a while statement, the fading effect will appear instantaneous to us, but you could also use an onEnterFrame event to actually be able to view the fading, like this:

 function fade(movie:MovieClip, fadePoint:Number, amount:Number):Void{     movie.onEnterFrame = function(){          if(movie._alpha > fadePoint){             movie._alpha -= amount;         }     } } //now the function is made, let's run it on a couple movies fade(myMovie_mc,50,5); fade(myMovie2_mc,20,2); 

For more on scripted animation, see Chapter 13, "The Movie Clip Object."

So far we have used functions to perform basic repetitive tasks using code that we want to consolidate and use whenever we want without having to rewrite the entire script. In the next section, you'll learn how to make functions even more valuable by returning data.




Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net