Johnny G Junior's NoteBook Treasures: Function() master.

Friday, April 18, 2008

Function() master.

Who doesnt love functions... really. they are mini programs YOU write up and excute them over and over and over and over again.... just to because you can.

Format.


function name(){
do this;
}


Usage:

name(); <- this will execute your mini program. ok.

lets write one right now.


function EvilStatement(){
trace("vampires are evil");
}


what did we just do. Well... we created a program. thats all. nothing at all will happen with you preview this (alt + enter). thats because you need to execute your program. to excute.. use the name you called it with the function symbol.


EvilStatement();


BOOoooM. when you preview this... it will say in the result box- vampires are evil.

what can you do with functions besides that? well... lots of things. i will briefly touch on a few more examples and you're on your own.

function EvilStatement(sentence:string){
trace(sentence);
}


what did i do now? well. ( ) this is like a container that you can make temp. variables to be filled for later. i made a variable called "sentence" and defined ":string" as a string. now when i execute this script, i have to fill it with what information for the container... and i can fill it with many different things. sentence is a variable filled with whatever i tell it to have JUST FOR THAT FUNCTION. it never comes out of it.


EvilStatement("vampires are evil");
EvilStatement("bad breath is evil");


now both things will go into the function one at a time and it will be contained in the variable sentence. then it will produce a result for each.. separate.

More advance example. Lets say you have to write a script for a on release button multiple times. you could write a function with a container waiting for a movieclip to fill... or i will just show you.

function ButtonAction (item:MovieClip){ //<- block a start
item.onRelease = function(){ //<- block b start
getURL("www.google.com");
} //< block b end
} //< blcok a end

ButtonAction(website_btn);
ButtonAction(search_btn);
ButtonAction(more_btn);


now each execution will fill the item with that name and move throught that script.

Party!!!!

No comments: