Creating a Function


Creating a function is as easy as using the keyword function, providing a name for the function, followed by a pair of parentheses for parameters (we discuss the topic of parameters in more depth later), then a colon, and the data type of the return value (return values are also discussed later in this chapter). After that, you can place whatever code you want between two curly brackets. It looks something like this:

 function myFunction():ReturnType{     //script to run in function } 

NOTE

ReturnTypes are not required, but it is good practice to have them for debugging purposes.


Now that you know what a function looks like, you can create one of your own. To begin, open a new Flash movie, click the first frame in the timeline, open the ActionScript panel (F2), and place the following code:

 function myFunction ():Void{     trace ("My first function"); } 

Now you have your own function, with a simple trace statement placed within the curly brackets. However, if you tested your movie at this point, nothing would happen. This is because all you have done so far is create a function; now you need to run it.

NOTE

Notice the Void identifier after the parenthesis. Because we are not returning any value, Void is used.





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