AS: Main
---------------------------
I wanted to make a section about recursive function calling, but I've decided I have to make one on functions first.
There are four types of programming
simple programming
procedural programming which we will now discuss
modular programming
object oriented programming (tutorial coming later
----------------------------
The idea behind procedural programming is that there are several actions that we wish to repeat more then once during our movies. Lets say for example we have the code:
if ((rot<0) && (rot>-180)) {
rot *= -1;
} else if (rot>0) {
rot = 180-rot;
rot += 180;
}
for those of you experianced with roation, this might seem fermiliar, it converts flash rotation to workable rotation (for trig functions)
now lets say we want the player to have this code, the player's turret to have this code, and the player's second turret to have this code.
without functions we would have to have the same unreadable code 3 times, with functionswe can save that.
another issue is readability, with functions your code could look like this
onClipEventLoad(){
initLocation();
initScore();
}
onClipEvent(enterFrame){
move();
if(attacking()==true){
attack();
}
checkHP();
}
isn't that nifty ;)?
---------------------
Actual use, a function and a procedure are different in 1 thing, a function has a return value, for example the hitTest function is a function because it returns either true or false, but trace is a procedure because it does not return anything.
Full function structure
function functionName(param:Type,param2:Type....paramN:Type):returnType{
//code
return returntType;//if there is no return type, no return command is needed
}
Patrial function structure
function functionName(param1,param2,param3....paramN){
//code
return anything; //return is optional
}
There is a third type called methods, they are very similar to functions only they work on the containers variables, in functions each variable you declare is a LOCAL VARIABLE, meaning you'll only have access to it within the function, and the local vars only have access to params and otehr local vars (and superglobals).
In methods however you use the this. expression to act using the container's properties, remmember, any param you change inside a function will not change outside of it.
I recommand the full decleration over the partial one, but it's only smart debugging wise.
---------------------------------------
Examples:
procedure:
function setStageAlpha(alpha:Number):Void{
_root._alpha=alpha;
}
function trace7times(expression:String):Void{
for(var i:Number=0;i<7;i++){
trace(expression);
}
}
function:
function toStringAdd(num1:Number,num2:Number,num3:Number):String{
return "The result is: " (num1+num2+num3);
}
function add(num1:Number,num2:Number):Number{
return num1+num2;
}
function isDownBoth(key:Number,key2:Number):Boolean{
return Key.isDown(key) && Key.isDown(key2);
}
methods example:
function moveX(){
if(Key.isDown(Key.RIGHT)){
this._x+=5;
}else if(Key.LEFT)){
this._x-=5;
}
}
functions may also call other functions, as long as they are in the same container...
ask any questions here