15 Replies Latest reply on Aug 13, 2007 9:58 AM by Man of Flash

    Variable variable names

    Man of Flash
      In AS3, can someone tell me how to create variable names with a variable. I would like to, for example, add an unknown number of videos to the stage as follows:

      for(i=0;i<numVideos;i++){
      ...
      var vid+i = new Video();
      ...
      }

      I would like the video names to be vid0,vid1,vid2, etc. I believe one could do this in previous versions of Actionscript with the
      eval() function; which is not available in AS3.

      Thank you
        • 1. Variable variable names
          .:}x-=V!P=-x{:. Level 3
          maybe this?

          for(var i:number=0;i<numVideos;i++){
          ...
          var ["vid"+i] = new Video();
          ...
          }

          or

          for(var i:number=0;i<numVideos;i++){
          ...
          _root["vid"+i] = new Video();
          ...
          }
          • 2. Re: Variable variable names
            Level 7
            > maybe this?
            >
            > for(i=0;i<numVideos;i++){
            > ...
            > var ["vid"+i] = new Video();
            > ...
            > }

            The array access operator, [], is definitely the key, but it doesn't
            work in conjunction with the var keyword. It's a way to get or set object
            properties by way of strings, so for example, this could work:

            for (var i:int = 0; i < numVideos; i++){
            ...
            this["vid"+i] = new Video();
            ...
            }

            > or
            >
            > for(i=0;i<numVideos;i++){
            > ...
            > _root["vid"+i] = new Video();
            > ...
            > }

            That's closer, but AS3 has no _root keyword, and the newer root
            substitution can't be used in the same way; that is, it can't be used to
            create new variables in the document's root.


            David Stiller
            Co-author, Foundation Flash CS3 for Designers
            http://tinyurl.com/2k29mj
            "Luck is the residue of good design."


            • 3. Re: Variable variable names
              Man of Flash Level 1
              This solutions is certainly closest:

              for (var i:int = 0; i < numVideos; i++){
              ...
              this["vid"+i] = new Video();
              ...
              }

              However, I still get an error that 'var0' (if numVideos is set to 1) is undefined. If I define var0 as a video before this, then it works. But then, that defeats the purpose, doesn't it?

              Thanks ...
              • 4. Re: Variable variable names
                Greg Dove Level 4
                hmmm. Don't you mean 'vid0' ? Might be a simple typo.

                'var0' would be undefined because it would create 'vid0'
                • 5. Re: Variable variable names
                  Man of Flash Level 1
                  You're right, I meant 'vid0'
                  • 6. Re: Variable variable names
                    Greg Dove Level 4
                    Are you getting a compiler error. I haven't started using as3 routinely yet, just been dabbling.
                    I just tried it and the compiler doesn't like the regular access as vid0 (which you can do in as2) because I presume it hasn't been declared and as3 is stricter. So you need to use the same type of access afterwards... there are others here who are 'veterans' in as3 (well it doesn't take much compared with me) who can probably explain better.

                    This code shows you it seems to work:.

                    • 7. Re: Variable variable names
                      Man of Flash Level 1
                      I don't think it's a variable declaration issue. Here is the code I'm using to add a video stream to the stage:
                      //--------------------------------
                      var vid0 = new Video();
                      //-------------------------------
                      var customClient0:Object = new Object();
                      customClient0.onCuePoint = cuePointHandler0;
                      customClient0.onMetaData = metaDataHandler0;
                      nc0 = new NetConnection();
                      nc0.connect(null);
                      ns0 = new NetStream(nc0);
                      ns0.addEventListener(NetStatusEvent.NET_STATUS, netstat0);
                      ns0.client = customClient0;
                      vid0.attachNetStream(ns0);
                      vid0.x = 100;
                      vid0.y = 100;
                      ns0.play("myvideo.flv");
                      addChild(vid0);

                      function netstat0(stats:NetStatusEvent){
                      }
                      function cuePointHandler0(infoObject:Object):void{
                      trace('cue:'+infoObject.time);
                      }
                      function metaDataHandler0(infoObject:Object):void{
                      trace('meta:'+infoObject.duration);
                      }

                      I would like to replace the first line with something like:

                      var numVideos:int=1;
                      for (var i:int = 0; i < numVideos; i++) {
                      this["vid"+i] = new Video();
                      }

                      to create a var named 'vid0' that has a new Video. The output error I get is: "Variable vid0 is not defined". It might simply be that I can't do this and that I'll have to create a seperate video instance for each of the possible number of videos that may be included (list of videos is from an xml file).

                      Thanks ...

                      • 8. Re: Variable variable names
                        Damon Edwards Level 3
                        try var vid0:Video = new Video()
                        • 9. Re: Variable variable names
                          Man of Flash Level 1
                          I'm not sure what you mean. I tried this:

                          var numVideos:int=1;
                          for (var i:int = 0; i < numVideos; i++) {
                          this["vid"+i]:Video = new Video();
                          }

                          but I get an Compiler error that "Label must be a simple identifier"
                          • 10. Re: Variable variable names
                            Damon Edwards Level 3
                            //--------------------------------
                            var vid0 = new Video();
                            //-------------------------------

                            change to

                            var vid0:Video = new Video()
                            • 11. Variable variable names
                              Man of Flash Level 1
                              That line works just fine. I want to replace that line with a way to dynamically create the variable name (so that the variable name is not hard-coded to be 'vid0')
                              • 12. Re: Variable variable names
                                Level 7
                                Man of Flash,

                                > That line works just fine. I want to replace that line with
                                > a way to dynamically create the variable name (so that
                                > the variable name is hard-coded to be 'vid0')

                                In this line ...

                                this["vid"+i]:Video = new Video();

                                ... that post colon suffice -- :Video -- is causing the grief. If you're
                                using the array access operator, you can't use the var keyword (which you
                                aren't) and you can't use string typing (which you are).


                                David Stiller
                                Adobe Community Expert
                                Dev blog, http://www.quip.net/blog/
                                "Luck is the residue of good design."


                                • 13. Re: Variable variable names
                                  Rothrock Level 5
                                  But you probably want to use var and typing because that is good practice. So the trick is that you don't need to name your instances when you create them.

                                  for (var i:int=0;i<11;i++){
                                  var vid:Video=new Video();
                                  vid.name="vid"+i;
                                  }

                                  Then to access them and all you will need to look for the methods that include something like getByName or what have you.

                                  You could also do something like this:

                                  var myVideos:Array=new Array();

                                  for(var i:int=0;i<11;i++){
                                  var vid:Video=new Video();
                                  myVideos.push(vid);
                                  }

                                  You could then just use the references in the array to assign various properties and stuff and such.

                                  I haven't done enough with AS3 to know all the benefits and drawbacks of the different ways to accomplish this yet. But these are the two ways I've found so far to deal with this.
                                  • 14. Re: Variable variable names
                                    .:}x-=V!P=-x{:. Level 3
                                    good info thanks
                                    • 15. Re: Variable variable names
                                      Man of Flash Level 1
                                      Thanks Rothrock. I used your array suggestion and that did the trick.