3 Replies Latest reply on Jul 12, 2006 1:24 PM by Newsgroup_User

    Dynamically adding multiple instances of a movie clip to the stage with one button

    muhl
      hello,

      I was wondering if there was a way to add several instances of the same movie clip to the stage dynamically utilizing one button.

      I can do one with the following code placed on the button...

      on (release) {
      attachMovie ("filledCircle", "filled1", 5);
      filled1._x = 370;
      filled1._y = 225;
      }

      But I want the user to be able to hit the button again and get yet another instance of "filledCircle" on the stage.

      I also want the user to be able to drag these instances around...

      Any help would be appreciated...

      Thanks,

      Muhl
        • 1. Re: Dynamically adding multiple instances of a movie clip to the stage with one button
          Level 7
          Muhl,

          > I was wondering if there was a way to add several
          > instances of the same movie clip to the stage
          > dynamically utilizing one button.

          Sure thing.

          > I can do one with the following code placed on the
          > button...
          >
          > on (release) {
          > attachMovie ("filledCircle", "filled1", 5);
          > filled1._x = 370;
          > filled1._y = 225;
          > }

          Gotcha.

          > But I want the user to be able to hit the button again
          > and get yet another instance of "filledCircle" on the
          > stage.

          You're in luck, because this isn't very hard to do. The main thing to
          keep in mind is that each instance must have A) its own unique instance name
          and B) its own unique depth. In your example, the instance name is filled1
          and the depth is 5. The next clip's instance name should be filled2 at a
          depth of 6. Then filled3, depth 7, and so on. You can use a single
          variable to handle the incrementation.

          // code in a frame
          var counter:Number = 1;

          // code on your button
          on (release) {
          attachMovie ("filledCircle", "filled" + counter, counter + 4);
          }

          With me so far? The variable counter contains the numeric value 1. The
          second parameter of attachMovie() is provided with a concatenation of
          "filled" + 1, which makes "filled1". The third parameter is provided with
          the sum of counter plus 4, which makes 5. Obviously, we need a bit more.
          The button must, in addition, increment the value of counter. The ++
          operator handles this perfectly.

          on (release) {
          attachMovie ("filledCircle", "filled" + counter, counter + 4);
          counter++;
          }

          Now, it seems you also want to position the attached movie clip to (370,
          225). Are they call supposed to go to the same place? If so, you may use a
          second variable to hold a reference to the newly attached clip. Look up
          MovieClip.attachMovie(), and you'll see that the method returns the exact
          reference you need.

          // code in a frame
          var counter:Number = 1;
          var mc:MovieClip;

          // code on your button
          on (release) {
          mc = attachMovie ("filledCircle", "filled" + counter, counter + 4);
          counter++;
          mc._x = 370;
          mc._y = 225;
          }

          Make sense?

          > I also want the user to be able to drag these instances
          > around...

          Then you need to handle a few events. You're dealing with movie clips
          here, so your best bet is to study up on the MovieClip class, which defines
          all movie clips. (Note, also, that the TextField class defines all input
          and dynamic text fields; the Sound class defines all sounds, etc. This is a
          very handy arrangement of the ActionScript 2.0 Language Reference.)

          // code in a frame
          var counter:Number = 1;
          var mc:MovieClip;

          // code on your button
          on (release) {
          mc = attachMovie ("filledCircle", "filled" + counter, counter + 4);
          counter++;
          mc._x = 370;
          mc._y = 225;
          mc.onPress = function() {
          this.startDrag();
          }
          mc.onRelease = function() {
          this.stopDrag();
          }
          }

          Easy as that. You're simply assigning a function literal to the event
          of each new MovieClip instance as you create it. Take a look and you'll see
          each of these class members available to you -- that is, to all movie clips.
          MovieClip.onPress, MovieClip.startDrag(), MovieClip._x, etc.

          Wherever it shows the term MovieClip in the Language Reference, just
          replace that with the instance name of your clip -- or a reference to that
          clip (which even includes the global "this" property).


          David
          stiller (at) quip (dot) net
          Dev essays: http://www.quip.net/blog/
          "Luck is the residue of good design."


          • 2. Re: Dynamically adding multiple instances of a movie clip to the stage with one button
            muhl Level 1
            David,

            Thanks, it works like a champ...I should have mentioned though that within the filledCircle mc I have an input text box for the user to enter a field of text. When I when I do the drag and drop code from the button, I can't type in the input test box. Is there a work around for this???

            Thanks again...I appreciate it...

            Mike
            • 3. Re: Dynamically adding multiple instances of a movie clip to the stage with one button
              Level 7
              Mike,

              > I should have mentioned though that within the
              > filledCircle mc I have an input text box for the
              > user to enter a field of text.

              Hummina, hummina ...

              > When I when I do the drag and drop code from
              > the button, I can't type in the input test box. Is
              > there a work around for this???

              Yeah, those mouse event handlers may be obscuring that. You could try
              this. Build the MC in question in such a way that it contains the text
              field on one layer and another movie clip beneath that -- beneath it on its
              own layer. Give that inner movie clip an instance name, and assign the
              press and release functions to that clip, instead.

              on (release) {
              mc = attachMovie ("filledCircle", "filled" + counter, counter + 4);
              counter++;
              mc._x = 370;
              mc._y = 225;
              mc.inner_mc.onPress = function() {
              this._parent.startDrag();
              }
              mc.inner_mc.onRelease = function() {
              this._parent.stopDrag();
              }
              }

              Note how the pathing changes. And honestly, I can't be sure this will
              do it for you, but it's worth a shot.


              David
              stiller (at) quip (dot) net
              Dev essays: http://www.quip.net/blog/
              "Luck is the residue of good design."