A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: calling Functions with a variable

  1. #1
    Member
    Join Date
    Apr 2002
    Posts
    76

    calling Functions with a variable

    Hi,
    Is there any way to call a function whose name is returned by a variable.
    say for example if u have three functions

    function function1(){
    trace("function 1");
    };

    function function2(){
    trace("function 2");
    };

    function function3(){
    trace("function 3");
    };

    and there is a variable x whose values will be "function1","function2","function3"

    the question is ..... is it possible to execute the function by value x
    like call(x); or something of that sort.


    Please get back ASAP.

    Regards
    Venkat

  2. #2
    Live And Let Live All sutar's Avatar
    Join Date
    Mar 2002
    Posts
    202

    Try this one...

    Hi JV

    what I understand , you want to call a function depending upon the value of the variable.

    check the value of the variable in loop or make it an array.

    Code:
    if(yourVarable[Func 1] =="function1"){
       function1();
    }else if(yourVarable[Func 2] =="function2"){
       function2();
    }else if(yourVarable[Func 3] =="function3"){
       function3();
    if this doesn't solves your problem..

    try attaching the file

    ______________________________________________


  3. #3
    Member
    Join Date
    Apr 2002
    Posts
    76
    Hi sutar,

    Thanks for your reply.....

    is there anything other than this nested if's ... cos I want this code to run in an onEnterFrame code... now if I put this it will severly degrade the performance ... which is why in the first case I asked for the call method.... any suggestions
    V E N K A T
    ~~~~~~~~~~~~~~~~~~~~~~~
    BEGIN WITH END IN THE MIND

  4. #4
    Live And Let Live All sutar's Avatar
    Join Date
    Mar 2002
    Posts
    202

    try using for loop

    JV try a loop

    Code:
    for(i=0;1<yourVarable.length; i++){
       if(yourVarable[eval("Func "+i)] == "function"+i){
        call(eval(function"+i+"()"));
       break; 
    }

    ______________________________________________


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center