Image Image Image Image Image

© Copyright 2012 ABCoder | Email | RSS

Scroll to Top

To Top

Actionscript 2

Calling a function randomly / arbitrarily

On 26, Jun 2009 | 5 Comments | In Actionscript 2, Flash | By

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]]();
}

Tags | , , , , ,

Comments

  1. Good contenent I must inform about it my dad as he is interested in it. Best Regards Bukmacherzy Promocje.

  2. First and foremost, is are you finding the main Feed and additionally secondly what precisely can’t My spouse and i? I truly need to improve audience in this case so might any person point me personally off to the right feed?

  3. Your Article about Calling a function randomly / arbitrarily Very fantastic visual appeal on this web site , I’d rate it 10 10.

  4. Calling a function randomly / arbitrarily I was suggested this blog by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my difficulty. You are amazing! Thanks! your article about Calling a function randomly / arbitrarily Best Regards Andy Lawrence

Submit a Comment