-
[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.
-
FK'n_dog
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);
};
-
Originally Posted by a_modified_dog
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 == 1){
root[c](arr[1]);
}else if(arr.length - 1 == 2){
root[c](arr[1],arr[2]);
}else if(arr.length - 1 == 3){
root[c](arr[1],arr[2],arr[3]);
}else if(arr.length - 1 == 4){
root[c](arr[1],arr[2],arr[3],arr[4]);
}else if(arr.length - 1 == 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.
-
FK'n_dog
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
-
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!
-
FK'n_dog
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 param) trace("param["+vars+"] = "+param[vars]);
};
hth
-
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
-
FK'n_dog
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);
-
Originally Posted by a_modified_dog
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|