Calling a function randomly / arbitrarily
Say you have 2 or more functions in your actionscript, for some reason you have to call one of them arbitrary and dynamically. You don’t know which function will call at run time. You can do it by this way….
var myfunctions:Array = new Array();
myfunctions[0] = "myfunc1";
myfunctions[1] = "myfunc2";
myfunctions[2] = "myfunc3";
function myfunc1 (){
trace("This is my function number 1");
}
function myfunc2 (){
trace("This is my function number 2");
}
function myfunc3 (){
trace("This is my function number 3");
}
function randRange(min:Number, max:Number):Number {
var randNum:Number = Math.round(Math.random()*(max-min))+min;
return randNum;
}
btn.onPress = function(){
var randomNumber = randRange(0,myfunctions.length-1);
_root[myfunctions[randomNumber]]();
}



My Elance Profile
Recent Comments