TwelvestoneFlash

call a function with eval()???


Sign in

  • Waiting for Godot ( 720 k posts )
    Just conversation.
  • Thunder Dome ( 23 k posts )
    Photoshop Tennis and Collabs.
  • Photography ( 4.8 k posts )
    For all you shutterbugs, sh...
  • Flash ( 18 k posts )
    ActionScripting to tweens, ...
  • Front End ( 5.8 k posts )
    general front end design an...
  • Back End ( 9.6 k posts )
    serverside scripting, progr...
  • Projects and Theory ( 12 k posts )
    This forum is for discussio...
  • FAQ ( 269 posts )
    All those nagging questions...
  • Design ( 17 k posts )
    graphics & all aspects of g...
  • Purgatory ( 3.6 k posts )
    12stone Jail, feel free to ...
sakri
 
2003-01-27

hi there,

I use this type of syntax every now and then to dynamically call functions (from incoming xml etc.):

function call_func(name,arg){ _rootname; }

function say_hi(message){ trace(message); }

call_func("say_hi","hello");

now I wanna do the same using eval:

name="say_hi"; val="hello"; eval(name+"("+val+");");

but I can't get it to work.... any ideas??? is it even possible???

persist
 
2003-01-27

interesting problem.

You may be able to get away with finding the function as an object wihtout a parameter such as this:

name="say_hi"; eval(name);

but I doubt it.

Why can't you just use the first method?

bit-101
 
2003-01-27

how bout

eval(name)(val);

trabus
 
2003-01-27

Cool! Can you explain why that works?

bit-101
 
2003-01-27

eval(name) returns a reference to the function. add the parentheses and an argument and you're off.

trabus
 
2003-01-27

Actually my lead programmer just explained it to me. He also said thisname; works as well.

function say_hi(message){ trace(message); }

name="say_hi"; val="hola"; eval(name)(val); // traces "hola" thisname; // traces "hola"

//edit> duh, didn't even notice that is how sakri is doing it in the first post..

bit-101
 
2003-01-27

yup. learned lots of neat tricks when i was running the 25 line contest. my favorite is to create or attach a movie clip and immediately call a method or change a property:

createEmptyMovieClip("thing", 0)._alpha=50;

attachMovie("thing", "thing1", 1).gotoAndPlay(100);

this works because the create/attach functions return a reference to the new clip, so the whole statement can be used as a reference to a movie clip.

sakri
 
2003-01-28

cheers bit101!!! just what I was after k

and yeah... those 25 lines... I got loads out of them!!!

trabus, if you like this type of stuff... check this out:

MovieClip.prototype.draw_square=function(x,y,w,h,col){ this.lineStyle(0,0xFFFFF,0); this.beginFill(col,100); this.moveTo(x,y); this.lineTo(x,y); this.lineTo(x+w,y); this.lineTo(x+w,y+h); this.lineTo(x,y+w); this.lineTo(x,y); this.endFill(); }

XML.prototype.do_actions=function(){ var node=this.firstChild.childNodes[0]; var targ=node.attributes.target; var a=0; var b=node.childNodes.length; while(a<b){ var prop=node.childNodes[a].nodeName; if(node.childNodes[a].attributes.dtype=="int"){ var val=parseInt(node.childNodes[a].firstChild.nodeValue); }else{ var val=node.childNodes[a].firstChild.nodeValue; } _root[targ][prop]=val; a++; } }

createEmptyMovieClip("square",1); square.draw_square(0,0,100,100,0xDDCC88);

s=""; s+=""; s+= ''; s+= '<_x dtype="int">200</_x>'; s+= '<_y dtype="int">100</_y>'; s+= '<_xscale dtype="int">300</_xscale>'; s+= '<_yscale dtype="int">230</_yscale>'; s+= '<_rotation dtype="int">45</_rotation>'; s+= ''; s+=''; my_xml=new XML(s); my_xml.do_actions();

just paste it into flashMX and testmovie...

this is a "light" version... you can equally call custom functions or methods of objects like this... in theory you could construct a whole page like this... (hehe... look atwww.protosite.net k ) I havent had time to work on that for ages, but the idea is that the swf loads up xml, processes it and outputs a page... a bit like xml and xslt...

Sorry, you must be a member to post to a conversation. Either log in or sign up to get involved.
TwelvestoneFlash

call a function with eval()???