-
AS3 – how to do eval() the right way
Posted on June 27th, 2008 13 commentsIn AS2 we used to be able to refer to an instance name of an object by coding:
///AS2.0\\\
eval("my_mc"+i);This would allow you to create and/or refer to instance names using a string. I have heard quite a bit of negativity about eval() but it did come in handy at times. In AS2, you could also use:
///AS2.0\\\
_root["my_mc"+i];which worked well for me when pushing items to an array, before AS3 was available.
Now, the correct AS3 way to do it is either this:
///AS3.0\\\
this["my_mc" + i];or:
///AS3.0\\\
this.getChildByName("my_mc" + i);I would recommend the second way of doing it, because to me it just makes more sense.
13 responses to “AS3 – how to do eval() the right way”
-
testor April 30th, 2009 at 06:37
Hey great !!! after 20 min searching on google, you are the only one giving a REAL solution to this problem(solution 2 is the best yeah
)Thanks a lot !
-
Glad I could help!
-
davidesieb July 28th, 2009 at 03:13
Hello, but how make AS3 “evaluate” a variable value?
Say I have a variable:
var vMyExampleVar_X:Number = 100;
var vMyExampleVarTwo_X:Number = 50;and I want to dynamically access to it :
function fReturnVarValue (vVarName) {
var vValue = this["v"+vVarName+"_X"];
return vValue;
}
//
fReturnVarValue (“MyExampleVar”);
fReturnVarValue (“MyExampleVarTwo”);-> this doesn’t work… See what I mean?
Thanks for your help -
I tested your code that you wrote and it seems to work just fine that way. created variables from your function and traced them and they are returning correctly – see the updated code below
var vMyExampleVar_X:Number = 100;
var vMyExampleVarTwo_X:Number = 50;
//
function fReturnVarValue (vVarName) {
var vValue = this["v"+vVarName+"_X"];
return(vValue);
}
//
var newVar1:Number = fReturnVarValue ("MyExampleVar");
var newVar2:Number = fReturnVarValue ("MyExampleVarTwo");
trace (newVar1)
trace (newVar2)Hope this helps you – let me know if you have any other troubles.
-
Vinay July 30th, 2009 at 02:06
Hi Thanks a lot for wonderfull solution
-
Thanks!, this helps so much…but i have a new question. Im trying to create a new instance dinamicaly, like this:
———————————-
….
var slidesNum:int = 1;
var mcString = “mc”+1;
var oMc1:MovieClip = new[mcString]();
….
———————————-and it returns this:
TypeError: Error #1007: Instantiation attempted on a non-constructor.should this works?
Im really stuck… can u give-me a little hand?..(or a entire arm?)
-
i try this too
———————————-
….
var slidesNum:int = 1;
var mcString = “mc”+1;
var oMc1:MovieClip = new this[mcString]();
….
———————————-the result is the same!
-
Try this out:
var slidesNum:int = 1;
var mcString:String = “mc”+1;var oMc1:MovieClip = new MovieClip();
oMc1.name = mcString;Let me know if that is what you are trying to do. Without seeing more of your code I am not sure if that is what you will need.
-
David OBrien March 16th, 2010 at 13:49
I have a string “per” and then a precentage like 0,10,20,30 etc
example “per100″
have embedded images
[Embed(source="0.gif")] [Bindable] public var per0:Class;
…
[Embed(source="100.gif")] [Bindable] public var per100:Class;I want the source=”{ blah }” of an image to
be the per100 embedded imagebut I can’t get it to work
-
Would need a bit more information / code from you in order to help you out.
-
try
var slidesNum:int = 1;
var mcString = “mc”+1;
getDefinitionByName(mcString) as Class; -
why wont this work April 26th, 2012 at 18:17
why dont this work
addEventListener(Event.ENTER_FRAME,myFunction);
addEventListener(KeyboardEvent.KEY_DOWN,myFunction);
var runs:int = 0;
var slimers:Array = new Array();
slimers.push(“slimer1″);slimers.push(“slimer2″);
slimers.push(“slimer3″);
function myFunction(event:Event) {
slimer1.x = mouseX;
slimer1.y = mouseY;
runs++;
for(var j:Number = 3; i > 0;i–){
//do nothingfor(var i:Number = 0; i < 3;i++){
//var temp1:eval = eval(slimers[i]);
//var temp2:eval = eval(slimers[j]);
if(this[slimers[i]].hitTestObject(this[slimers[j]])){
this[slimers[i]].x = Math.random + 1 * 400;
this[slimers[i]].y = Math.random + 1 * 300;
this[slimers[j]].x = Math.random + 1 * 400;
this[slimers[j]].y = Math.random + 1 * 300;;
}
}}
} -
Thanks for the comment, but I will need to have more info about what is not working to help you out…
Leave a reply
-


