9 Replies Latest reply on Apr 3, 2009 8:03 AM by Newsgroup_User

    Creating an object name on the fly?

    Handycam Level 1
      I have a function (attached) which instantiates a movie clip class.

      I want to create these objects as needed by calling the creator as in:
      public function xmlLoaded(event:Event):void
      {
      myXML = XML(myLoader.data);
      makeBubble(0);
      }

      I works fine, except I want to generate the NAME using the passed uint as well, such as "bubble0" and "bubble4". How do I do this?

      Thanks.
        • 1. Re: Creating an object name on the fly?
          Peter Hahmann Level 1
          This should work. I couldn't understand why it's not working.

          var bName:String = "bubble"+i.toString();

          Regards,
          peter
          • 2. Re: Creating an object name on the fly?
            Handycam Level 1
            Well, that part works, but then how to I make that the object's name?

            In the very next line, where I do:
            var bubble1:ProductBubble = new ProductBubble(pName,pPrice,pLink,pThumb,292,90);

            I cannot use the name I made like:
            var bName:ProductBubble = new ProductBubble(pName,pPrice,pLink,pThumb,292,90);

            since it is a duplicate variable name, and it fails.
            • 3. Re: Creating an object name on the fly?
              Peter Hahmann Level 1
              Okay, understood.

              What you need to do is the following, lets suppose you have 3 objects that should be "bubbles";

              private var arr:Array = ["bubble01","bubble02","bubble03"];
              public var bubbles:Array = [];

              private function createBubbles():void{
              var bubble:Bubble;
              for each(var name:String in arr)
              {
              bubble = newBubble();
              bubble.name = name;
              this.addChild(bubble);
              bubbles[name] = bubble;
              }

              }

              private function explodeMyBubble():void{
              accessBubble("bubble01").explode();
              }

              private function accessBubble(name:String):Bubble{
              return bubbles[name];
              }


              Regards,
              Peter
              • 4. Re: Creating an object name on the fly?
                Handycam Level 1
                Thank you. I understand everything but:
                bubbles[name] = bubble;

                What is that for, exactly?
                • 5. Re: Creating an object name on the fly?
                  Level 7

                  "Handycam" <webforumsuser@macromedia.com> wrote in message
                  news:gr33l3$71d$1@forums.macromedia.com...
                  > Thank you. I understand everything but:
                  > bubbles[name] = bubble;
                  >
                  > What is that for, exactly?

                  Basically, what he's doing there is what a repeater does for you...He's
                  storing a reference to the dynamically created object, except where a
                  repeater will store the reference in an array index, he's using the array as
                  a dynamic object and storing the references in the "object" side of the
                  array. He's probably doing this because in ActionScript 1, this type of
                  task was typically done by an "associative array", or what is called in some
                  languages a "hash table." ActionScript 3 allows this because an Array is an
                  Object under the covers, but also has the side that you're "supposed" to use
                  (the true Array), where all of the items are accessed by index, not by name.
                  If you use the Object side of an Array, you can't use any of the Array
                  methods, because the values are there, but the useful part of the Array
                  doesn't know anything about them.

                  If you're not used to using Objects, they are essentially a blank canvas
                  that you can attach new properties to at will, so when you say
                  bubbles[name]=bubble, you are saying "add a new property to the bubbles
                  array called "bubble01" (or 02 or whatever) and put a reference to the new
                  bubble that was just created in it.

                  This would be a bit more clearly written like this:

                  private var arr:Array = ["bubble01","bubble02","bubble03"];
                  public var bubbles:Object= [];

                  private function createBubbles():void{
                  var bubble:Bubble;
                  for each(var name:String in arr)
                  {
                  bubble = newBubble();
                  bubble.name = name;
                  this.addChild(bubble);
                  bubbles[name] = bubble;
                  }

                  }

                  However, since you're probably naming the bubbles like that so that you can
                  access them numerically anyway, why not just use:

                  public var bubbles:Array = [];

                  private function createBubbles(numBubbles:int):void{
                  var bubble:Bubble;
                  for (var i=0;i<numBubbles;i++)
                  {
                  bubble = newBubble();
                  bubble.name = 'bubble0'+i;
                  this.addChild(bubble);
                  bubbles = bubble;
                  }

                  }

                  And then when you want to do something with a bubble, just do something like
                  this:

                  var bubbleIwant:int=0;
                  myBubble:Bubble=bubbles[bubbleIwant] as Bubble;
                  myBubble.doSomething();

                  Or use a repeater, which will create the bubbles and put them in an Array
                  for you.

                  HTH;

                  Amy


                  • 6. Re: Creating an object name on the fly?
                    Peter Hahmann Level 1
                    As your bubble is run-time created you can't access it while scripting without having compiler errors. The name is not your id, so to avoid errors you'll need to use getChildByName("bubble01"). But what happens if you create lots of containers in the same way? You'll have a sequence of getChildByName() to finally get your bubble. But if you store your bubble in an array when accessing it array[bubble_name] it will return your bubble directly. I don't know how its called in English, but i guess it would be an associative array. Your index wouldn't be a number, but would be a string, like a dictionary more or less. Does this make sense?

                    Regards,
                    Peter
                    • 7. Creating an object name on the fly?
                      Handycam Level 1
                      thanks, this is helpful.

                      what I really need to know, and I knew it once but cannot locate any examples, is access a name on the fly.

                      For exampe, let's say I have a group of objects on the stage "mything1", "mything2" etc

                      I want to access one of them by name when the incremental number is something passed from somewhere else, perhaps like:

                      var someNumberFromsomewhere:int = 2;

                      doSomething(someNumberFromsomewhere);

                      private function doSomething(i:int):void {
                      mything+i.blowYourselfUp();
                      mything+i.someValue = 14;
                      }

                      Make sense? I need to refer to the instance name root PLUS some number (or maybe string chunk) but I do not remember how to concatenate them.

                      • 8. Re: Creating an object name on the fly?
                        Handycam Level 1
                        This seems to work:

                        private function hiliteHandler(e:Event):void{
                        var t:uint = e.currentTarget.bubbleNumber;
                        this["number"+t].gotoAndPlay("over");
                        }

                        Bubble 1 tells the SWF named "number1" to go to its frame "over"
                        • 9. Re: Creating an object name on the fly?
                          Level 7

                          "Handycam" <webforumsuser@macromedia.com> wrote in message
                          news:gr5775$qm1$1@forums.macromedia.com...
                          > This seems to work:
                          >
                          > private function hiliteHandler(e:Event):void{
                          > var t:uint = e.currentTarget.bubbleNumber;
                          > this["number"+t].gotoAndPlay("over");
                          > }
                          >
                          > Bubble 1 tells the SWF named "number1" to go to its frame "over"

                          That will definitely work for components created in MXML, because the
                          compiler creates a variable called the same thing as the object's ID, which
                          you can then reference as you did above.

                          See http://www.magnoliamultimedia.com/flex_examples/Amys_Flex_FAQ.pdf Q10