4 Replies Latest reply on Feb 8, 2009 12:39 PM by Newsgroup_User

    Creating multiple instances

    SiHoop Level 1
      Below are two methods to create multiple instances. Clearly, method 2 is more efficient. However, if I use method 2 I don't know how to modify a specific instance later in my code. For example, using method 1 I can just write:
      tf1.alpha=0
      but I don't know how I can target a specific instance using method 2. Writing tf.alpha=0 will only target my last instance. How do I put something in my loop to allow me to identify every instance by name?

        • 1. Re: Creating multiple instances
          Level 7

          "SiHoop" <webforumsuser@macromedia.com> wrote in message
          news:gmkkn3$ok8$1@forums.macromedia.com...
          > Below are two methods to create multiple instances. Clearly, method 2 is
          > more
          > efficient. However, if I use method 2 I don't know how to modify a
          > specific
          > instance later in my code. For example, using method 1 I can just write:
          > tf1.alpha=0
          > but I don't know how I can target a specific instance using method 2.
          > Writing
          > tf.alpha=0 will only target my last instance. How do I put something in my
          > loop
          > to allow me to identify every instance by name?
          >
          >
          >
          > Method 1:
          > var tf1:TextField=new TextField
          > var tf2:TextField=new TextField
          > var tf3:TextField=new TextField
          > tf1.text=myArray[0]
          > tf2.text=myArray[0]
          > tf3.text=myArray[0]
          > tf1.setTextFormat(format4)
          > tf2.setTextFormat(format4)
          > tf3.setTextFormat(format4)
          >
          > Method 2:
          > for(var i:uint:=0;i<3;i++){
          > var tf:TextField=new TextField
          > tf.text=myArray
          > thisi["tf"+i].setTextFormat(format4)
          > }
          >

          var instances:Array=new Array();
          for(var i:uint:=0;i<3;i++){
          var tf:TextField=new TextField
          tf.text=myArray

          thisi["tf"+i].setTextFormat(format4)
          instances.push(tf);
          }

          HTH;

          Amy


          • 2. Creating multiple instances
            injpix Level 3
            Method 2 has better readability comparing it to Method 1. But I wouldn't say its more "efficient". Method 1 will give you slightly better performance. Because the VM doesn't have to: iterate; check the conditional; or increment the variable for the for-loop. But yes, I would use Method 2 (readability) over Method 1 (performance).

            Wouldn't you need to add (perhaps using addChild() ) these TextFields to something? If so you can assign a unique value for the 'name' property and then retrieve by that value.
            • 3. Re: Creating multiple instances
              SiHoop Level 1
              Amy,
              Thanks for the reply. I want you to know that your answer, although brief, has made an enormous difference for me. I've been wondering for a while how to store and use multiple instances and placing them into an array makes perfect sense. Simple but important.
              • 4. Re: Creating multiple instances
                Level 7

                "SiHoop" <webforumsuser@macromedia.com> wrote in message
                news:gmnesq$5sl$1@forums.macromedia.com...
                > Amy,
                > Thanks for the reply. I want you to know that your answer, although brief,
                > has
                > made an enormous difference for me. I've been wondering for a while how to
                > store and use multiple instances and placing them into an array makes
                > perfect
                > sense. Simple but important.

                You're welcome :-)