6 Replies Latest reply on Mar 13, 2007 12:17 PM by supdun

    Dynamically create a Sprite

      inside a for loop I would like to dynamically create new Sprites. How could I accomplish this task. Any help is much appreciated.
        • 1. Re: Dynamically create a Sprite
          DZ-015 Level 1
          if you have your sprite as a movie clip with appropriate linkage setup you can use attachMovie() like so...

          for(var i:Number = 0; i < 10; i++)
          attachMovie("idHappySprite", "mcHappySprite" + i, this.getNextHighestDepth());

          I hope this helps. ......if you want to get more information out of the kind folks on these forums you may want to be a bit more specific in your question. For instance, give us code snippets.
          • 2. Re: Dynamically create a Sprite
            supdun Level 1
            I apologize for the lack of details. This was meant to be a general question. I see your example, and I would like to make a similiar effect, but using Sprites in AS3. You see what I am looking into doing is creating a button (Simplebutton) and extending it to a list feature. Meaning, depending on how many buttons i want on the list. In order to accomplish this i would have to duplicate the same class and address there properties as needed. Again, this is using Action Script 3.0. Thanks for your reply, and if you or anyone else could help out...I would greatly appreciate it.
            • 3. Re: Dynamically create a Sprite
              Level 7
              Look at the SimpleButton example in the AS3 Language Reference. Put that
              code into a loop:

              import flash.display.*
              var maxButtons:Number = 10;

              for(var i:Number = 0; i < maxButtons; i++)
              var btn:SimpleButton= new SimpleButton();

              /* see example */


              /* move the button to wherever you need it */


              • 4. Re: Dynamically create a Sprite
                supdun Level 1
                Thanks, Raymond. Now I would like to change the position of each of those buttons. If you or anyone else can, check out my attached code. I tried using the random function, but it only moves on button.
                • 5. Re: Dynamically create a Sprite
                  Level 7
                  Except for the very last button, none of them have any content, so you're
                  just not seeing them. Move your button state assignments into the loop.

                  • 6. Re: Dynamically create a Sprite
                    supdun Level 1
                    Ah nice, thanks alot Raymond. Much appreciated. I attached the final code, just in case anyone else want to test.