my interests and experiences
RSS icon Email icon Home icon
  • AS3 – how to do eval() the right way

    Posted on June 27th, 2008 whodeee 15 comments

    In AS2 we used to be able to refer to an instance name of an object by coding:


    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:


    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:

    this["my_mc" + i];


    this.getChildByName("my_mc" + i);

    I would recommend the second way of doing it, because to me it just makes more sense.


    15 responses to “AS3 – how to do eval() the right way”

    1. 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 !

    2. Glad I could help!

    3. 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

    4. 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"];
      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.

    5. Hi Thanks a lot for wonderfull solution

    6. 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?)

    7. i try this too
      var slidesNum:int = 1;
      var mcString = “mc”+1;
      var oMc1:MovieClip = new this[mcString]();

      the result is the same!

    8. Try this out:
      var slidesNum:int = 1;
      var mcString:String = “mc”+1;

      var oMc1:MovieClip = new MovieClip(); = 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.

    9. 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 image

      but I can’t get it to work

    10. Would need a bit more information / code from you in order to help you out.

    11. try

      var slidesNum:int = 1;
      var mcString = “mc”+1;
      getDefinitionByName(mcString) as Class;

    12. why wont this work

      why dont this work
      var runs:int = 0;
      var slimers:Array = new Array();

      function myFunction(event:Event) {
      slimer1.x = mouseX;
      slimer1.y = mouseY;
      for(var j:Number = 3; i > 0;i–){
      //do nothing

      for(var i:Number = 0; i < 3;i++){
      //var temp1:eval = eval(slimers[i]);
      //var temp2:eval = eval(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;;


    13. Thanks for the comment, but I will need to have more info about what is not working to help you out…

    14. Can I evaluate a function call in a string, not just variable?

    15. Thanks a lot!!!
      You are a genius!!!
      I have a very complicated program and I was crying for not to find something that could replace eval function

    Leave a reply