FUNCTIONS
Functions help make code simpler by modularizing blocks of code together to be executed OVER and OVER
when called upon.
You create a function by using the word "function", followed by the nifty name you create
for your function. This is what you will call it by whenever you want it to do what you put between the curly
brackets.
The "argument" is what you put between the (). This is data that you can send to the function so it can
do something with it, like a mathematical calculation.
function nameOfFunction(parameterToPass1:Number, parameterToPass2:String)
{
doSomething = parameterToPass1+parameterToPass2;
}
when you need the function, just call!
nameOfFunction(3000, "Heavy Duty");
Here is a simple example that increments a "score" when buttons are pressed
var scoreX:Number=0;
butt1_btn.addEventListener(MouseEvent.CLICK, btn1);
butt2_btn.addEventListener(MouseEvent.CLICK, btn2);
function btn1(event:MouseEvent):void
{
scoreIt(1);
}
butt2_btn.addEventListener(MouseEvent.CLICK, btn2);
function btn2(event:MouseEvent):void
{
scoreIt(10);
}
function scoreIt(xvalue:Number)
{
scoreX=scoreX+xvalue;
score_txt.text=String(scoreX+" !!!!");
}
|