2 Replies Latest reply on Feb 25, 2007 9:04 PM by jpwrunyan

    AS3 -- _root["object_"+i] approach

    jeroencornelissen
      Hi,

      I'm experimenting in AS3 and I stumbled upon this little problem:

      /* --------- Projects are ready to be used! --------- */
      for(var i:uint=0;i<projectsArr.length;i++) {

      var proj:Project = projectsArr ;
      var imgStr:String = proj.getImageFromCategory(0,proj.getCategory(0));
      proj.loadImage(imgStr);

      ref = new UIComponent();
      addChild( ref );

      ref.x = 400 + 120*i;;
      ref.y = 450;
      ref.scaleX = .3;
      ref.scaleY = .3;

      ref.name = proj.projectName;

      proj.addEventListener(CustomDispatcher.ACTION,projectComplete);

      ref.addChild( proj );

      }

      So I've filled an array with Project objects that I now want to add to the stage. This works fine and it displays nice.
      But I can't access these projects anymore only the last one, because the ref:UIComponent is always overwritten.
      The ref is a public variable, because I need to use it in the projectComplete function (worked fine with one project object).

      In my AS1/2 days I would have used something like :
      _root["project"+i] = new UIComponent();

      Is there a way to do this in AS3?
        • 1. Re: AS3 -- _root[&quot;object_&quot;+i] approach
          _Albertx_ Level 1
          Well... if you lost the reference to some element in the displayList you can get it back with getChildAt(index).

          var someElement:DisplayObject = this.getChildAt(0);

          Another option is to use the method getChildren() of any Component that extends the class Container. This method gives you an array with the references to each element.

          var elements:Array = this.getChildren();

          With this, you will have the element references in each index of your array.
          • 2. Re: AS3 -- _root[&quot;object_&quot;+i] approach
            jpwrunyan Level 1
            No offense intended, but you probably should have stopped using "_root" since AS 2.0. I forbid people to use it on my projects. That said, I assume you had a really really good reason for needing it. ;-)

            Anyway, in AS 3.0 there is parentDocument, parentApplication, and Application.application for accessing the "root" object. See if any of those are what you are looking for.

            Finally, try not to post code with "[" "i" "]" in it. It becomes the italic tag and doesn't display. Put a space between the brackets like so: [ i ]. And then your posted code can be as close to the original as possible. I really wish Adobe would fix this with a "code" tag or something...