A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: [RESOLVED] Calling a function from a name in text box?

  1. #1
    Junior Member
    Join Date
    Nov 2007
    Posts
    24

    resolved [RESOLVED] Calling a function from a name in text box?

    I have a text box on the stage

    i want to be able to type the name of a function and it's arguments, click a button, and have the function executed with said argruments
    is this possible?

    Something like this:


    An input box and a button, button is clicked and function "function1" is called with argruments "hello" and 1
    alternatly i could have more than one text box, one for the function name, and others for the arguments, if that'd be easier

    thanks
    Last edited by JamesPoel; 08-08-2009 at 10:02 AM.

  2. #2
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    where you have function1("hello",1) in textfield instance - txt
    PHP Code:
    var a:String txt.text.split("(").join(",")
    var 
    b:String a.split(")").join("");
    var 
    arr:Array = b.split(",");
    var 
    c:String arr[0];
    root[c](arr[1],arr[2]);


    function 
    function1(n,m){
    trace(n+"  "+m);
    }; 

  3. #3
    Junior Member
    Join Date
    Nov 2007
    Posts
    24
    Quote Originally Posted by a_modified_dog View Post
    where you have function1("hello",1) in textfield instance - txt
    PHP Code:
    var a:String txt.text.split("(").join(",")
    var 
    b:String a.split(")").join("");
    var 
    arr:Array = b.split(",");
    var 
    c:String arr[0];
    root[c](arr[1],arr[2]);


    function 
    function1(n,m){
    trace(n+"  "+m);
    }; 
    Wow! What an amazing answer! thanks for taking your time to write that for me, it works well

    Thanks again!

    Edit: just another question, if you still read this thread, already marked it as resolved, oops!
    How would i handle a varied number of arguments?
    I have wrote this, and it works, but it seems like a bit of a long-winded way of doing it:

    PHP Code:
        if(arr.length == 1){
        
    root[c](arr[1]); 
        }else if(
    arr.length == 2){
        
    root[c](arr[1],arr[2]); 
        }else if(
    arr.length == 3){
        
    root[c](arr[1],arr[2],arr[3]); 
        }else if(
    arr.length == 4){
        
    root[c](arr[1],arr[2],arr[3],arr[4]); 
        }else if(
    arr.length == 5){
        
    root[c](arr[1],arr[2],arr[3],arr[4],arr[5]); 
        } 
    Thanks
    Last edited by JamesPoel; 08-08-2009 at 10:24 AM.

  4. #4
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    you can pass an array as the parameter to the function -
    PHP Code:
    var a:String txt.text.split("(").join(",")
    var 
    b:String a.split(")").join("");
    var 
    arr:Array = b.split(",");
    var 
    c:String arr[0];
    tmp arr.concat(); // copy the array
    tmp.shift(); // remove the first element - function1
    _root[c](tmp);


    function 
    function1(param){
    trace(param);
    trace(param[0]);
    trace(param[1]);
    }; 
    hth

  5. #5
    Junior Member
    Join Date
    Nov 2007
    Posts
    24
    that's great! Ok one last question, hope you dont hate me for asking so much. haha

    if i were to script in AS2 as apposed to AS3, the code still works, however what if i wanted to call a function found on a different level? such as _level2?

    i've tried changing root[c](arr[1],arr[2]); to _level2[c](arr[1],arr[2]); and typing _level2.function1("hello",hi) in the text box but it doesnt work :/ I hope you know what i mean, and i've learnt that the _level stuff is gone in AS3 so sorry for the AS2 question!

  6. #6
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    no probs
    i have 2 swf files - main.fla and sub.fla

    in main.fla -
    textfield - instance name - txt --- text = _level2.function1(hello,hi,1,2,help)
    button - instance name - btn

    in sub.fla -
    function1

    code in main.fla -
    PHP Code:
    loadMovieNum("sub.swf"2); // load sub.swf to _level2

    btn.onRelease = function(){
    var 
    a:String txt.text.split("(").join(","); 
    var 
    b:String a.split(")").join("");
    var 
    c:String =  b.split(".").join(",");
    var 
    arr:Array = c.split(","); 
    var 
    d:String arr[0];
    var 
    e:String arr[1]; 
    tmp arr.concat();
    tmp.shift(); 
    tmp.shift();
    _root[d][e](tmp); 
    }; 
    code in sub.fla -
    PHP Code:
    function function1(param){ 
    for(
    vars in paramtrace("param["+vars+"] = "+param[vars]);
    }; 
    hth

  7. #7
    Junior Member
    Join Date
    Nov 2007
    Posts
    24
    can i be really cheeky and ask one final question? :$
    what if function1 isn't on the main timeline of the sub swf? what if its inside one (or more) movieclips?

    it's alright if you cant answer that, thanks for your help with this i've learnt a lot

  8. #8
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    functions should always be declared on the main timeline
    if your clip has instance name - mc
    and textfield is - _level2.mc.function1(hello,hi,1,2,help)

    you will need to extend the script to accomodate it -

    PHP Code:
    var d:String arr[0]; // = _level2
    var e:String arr[1]; // = mc
    var f:String arr[2]; // = function1
    tmp arr.concat(); 
    tmp.shift(); // remove _level2
    tmp.shift(); // remove mc
    tmp.shift(); // remove function1
    _root[d][e][f](tmp); 

  9. #9
    Junior Member
    Join Date
    Nov 2007
    Posts
    24
    Quote Originally Posted by a_modified_dog View Post
    functions should always be declared on the main timeline
    if your clip has instance name - mc
    and textfield is - _level2.mc.function1(hello,hi,1,2,help)

    you will need to extend the script to accomodate it -

    PHP Code:
    var d:String arr[0]; // = _level2
    var e:String arr[1]; // = mc
    var f:String arr[2]; // = function1
    tmp arr.concat(); 
    tmp.shift(); // remove _level2
    tmp.shift(); // remove mc
    tmp.shift(); // remove function1
    _root[d][e][f](tmp); 
    Hello thanks i managed to get it to do exactly what i wanted it to! Thanks so much for your help

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