Action Scripting 3.0

Alejandra Jarabo
MAT 116

Media Arts & Technologies. Santa Barbara City College
PROGRAMING CONCEPTS

FUNCTIONS

FUNCTIONS are reusable blocks of code stored under a name, that are easy to be called back in Action Scripting.
Functions have worked in all versions of Action Scripting but now are going to be more present in AS 3.0

For absolute beginners, you´ll recognize a function because of the pair of parenthesis right after the name.

myFunction();

   
WRITING A CUSTOM FUNCTION  


The first time you introduce a function in a piece of code, you need to DECLARE the new function, including: the word function, the name of the function, the parenthesis and the data type.

The name of the function should be oneword with no spaces, easy to remember or identify later. The first letter always in lowercase (you may have capitals through the name, like in many Action Script terms).

If the name of the function turns blue after you type it, that word is already used by flash as a keyword and cannot be used as a function name.

Remember: since you invent the name of the function, it will always show up in black. Only Flash terms show up in blue.
(this is a good trick to find your function in the code)

function playBlueMovie ( ) : void
{
//Block of code goes here
}

After that, you can list the tasks the function will perform(one or more lines of code), within the opening and closing brace.

function
name of function () : data type
function
playBlueMovie () : void


See the function integrated in the button's code. Please visit the "How to program a button" page after you finish this page. LINK

 

function goNow(event:MouseEvent)
{
gotoAndPlay(5);
}

play_btn.addEventListener(MouseEvent.CLICK, goNow);

 

 
   
CALLING A CUSTOM FUNCTION  

Later in the code, when you want the function to run again, you will type the name of the function and then the pair of parenthesis, with the semicolon for end of the line.

(this is how you simplify the job: by not having to always write down the whole task the function contains)

If I have already DECLARED a function called playBlueMovie, Y will call it in the code by just writing its name followed by parentlesis: 

playBlueMovie();

 

 
   
WHAT ARE METHODS IN FLASH

Flash has organized commonly used tasks inside ¨pre-built functions¨ that you constantly use in Action Script.
They are called METHODS.

Examples of methods:

  play ( );

  stop ( );

  gotoAndPlay ( n );

  gotoAndStop ( n );

  prevFrame ( );

  nextFrame ( );


Notice that, since they are pre-built, we never see the actual code for them.

Some methods don´t need any extra information to run, like play() or stop()
Some other methods need some extra information called parameters, and you placed them within the parenthesis.

See some different parameters the method gotoAndPlay can use:

gotoAndPlay(20);

gotoAndPlay(¨begin¨);

gotoAndPlay(¨secondScene¨,3)