PDA

View Full Version : calling a function based on variable's value



natronp
February 16th, 2005, 11:09 PM
say I want to declare a global variable in my main movie on a mouse event (button) and give it a value "functionA".

then I want to load an external .swf into a clip (with the same mouse event)

then I want some code in the loaded .swf to execute a function of the same name "functionA"

how would I code this?

this way I could load the same .swf but execute different functions depending on which button triggered the load... comprende?

hope that makes sense... once I figure this out i will feel like i can actually make some stuff happen...

glkngs
February 17th, 2005, 02:54 AM
There might be a more "high-tech" way of doing it, but how about a simple if/else or maybe a switch case?
Something like

switch(func){
case "functionA" :
functionA(param);
break;
case "functionB" :
functionB(param);
break;
}
}

claudio
February 17th, 2005, 05:45 AM
Somerthing like this?
// declare your global variable
_global.a = "myFunc";
// your function
function myFunc() {
trace("AHHHH!");
}
// call the function
eval(a)();

brainy
February 17th, 2005, 07:59 AM
Eval is evil. Why not just set the variable to the function reference!?


function func1() {
trace(1);
}
function func2() {
trace(2);
}

_global.a=func1;
_globa.a();
_global.a=func2;
_globa.a();

virusescu
February 17th, 2005, 10:35 AM
brainy is hungry .. is eating letters :D
and as an extent to what brainy said this is the same thing

function myfunc() {
trace("something");
}

_global.a=myfunc();
_global.a;

claudio
February 17th, 2005, 10:45 AM
brainy is hungry .. is eating letters :D
and as an extent to what brainy said this is the same thing

function myfunc() {
trace("something");
}

_global.a=myfunc(); //outputs "something"
_global.a;
That's not the same because your're actually executing the function with _global.a=myfunc();