6 Replies Latest reply on Oct 16, 2011 8:56 AM by Ned Murphy

    Generate dynamic objects in Flash

    mikelaurey

      I'd like to use a conditional loop to generate objects dinamically and i'd like to know if it is possible to do this in Flash.

      I try to explain better what i want to do; i have to let users to choose a number of balls to display on the stage, something like this:

       

      var balls; //it contains the numbers picked by the user

      if ( balls == 0) display nothing on the stage;

      if ( balls != 0){

      for (n < balls){

      plot a ball on the stage

      }

      }

       

      How it is possible to generate objects by looking on user decisions?

       

      At the moment, i just know how to add a given number of objects on the stage and how to arrange them, but i do not know how to generate them depending on the user choices.

       

      Can you help me?

      I tryed to search on google, but i found nothing: what kind of tutorial should i read to accomplish this task?

       

      Thanks to all.

        • 1. Re: Generate dynamic objects in Flash
          Ned Murphy Adobe Community Professional & MVP

          Since you say you know how to add them, I assume you mean you are familiar with the attachMovie method.

           

          To do it based on some user input, it depends on what form of input that is, but if the user defines there to be "n" objects added, then you would use something to the effect of...

           

          for(i=1; i<=n; i++){

             this.attachMovie(...etc...)

          }

          • 2. Re: Generate dynamic objects in Flash
            mikelaurey Level 1

            Very simply solution! Thanks!

             

            But what about the absolute position of each object?

            With attachMovie I can display them on the stage, but what i have to do if i want to locate each object in a different position?

            • 3. Re: Generate dynamic objects in Flash
              Ned Murphy Adobe Community Professional & MVP

              If you look at the help documents there is an example there showing one way to do it, as part of the attachMovie parameters.  Otherwise, you can assign the _x/_y prperties after yo have called attachMovie

              1 person found this helpful
              • 4. Re: Generate dynamic objects in Flash
                mikelaurey Level 1

                Yes, i see that i can use:

                 

                this.attachMovie("linkageName", "instance2", this.getNextHighestDepth(), {_x:100, _y:100});

                 

                and specify x and y coordinates for each object, but is there any method to assign the position automatically given an offset?

                Because i do not know how many balls the user will want to add to the stage, so i have to choose the coordinates dinamically.

                • 5. Re: Generate dynamic objects in Flash
                  mikelaurey Level 1

                  I'm using this code for testing:

                   

                  on (release) {

                      for(i=1; i<=3; i++){

                   

                     main.attachMovie("att1", "newname1", 1, {_x:100, _y:100});

                     main.attachMovie("tavolo", "prova", 1);

                   

                  }

                   

                  but when i press the button, it just add "att1" on the stage and not the other items that i expect.

                  • 6. Re: Generate dynamic objects in Flash
                    Ned Murphy Adobe Community Professional & MVP

                    The first thing you should do is get the code off of the button and into the timeline.  Placing code on objects is not a healthy practice. Also, you do not want to place the object is the same depth... they cannot co-exist there.  Give the button an instance name (maybe "btn"), and in the timeline use the code...

                     

                    btn.onRelease = function(){  

                         for(i=1; i<=3; i++){ 

                            main.attachMovie("att1", "newname"+i, main.getNextHighestDepth(), {_x:100, _y:100});

                            main.attachMovie("tavolo", "prova"+i, main.getNextHighestDepth());

                         }

                    }

                    1 person found this helpful