8 Replies Latest reply on Oct 11, 2009 8:50 AM by kglad

    assigning AS to attached movie clips

    Shad0wdrag0n Level 1

      I have a file where movie clips are added to the stage at random time intervals and locations. How do I get pieces of code to effect all the created movie clips? At the moment I'm using attachMovie to add the movie clips to the stage. Once a movie clip is added I want to add three actions to it: the first makes it so that clicking on the movie clip will remove it from the stage, the second makes the movie clip move in a random direction across the stage, and the third makes it so that when the movie clip reaches the end of it's own timeline it performs an action then removes itself. Is there a tutorial on how to do something like this, or is it actually very simple to do?

        • 1. Re: assigning AS to attached movie clips
          kglad Adobe Community Professional & MVP

          create a function or functions that do those things:

           

          function removeF(mc:MovieClip){

          mc.onRelease=function(){

          this.removeMovieClip();

          }

          }

           

          function moveF(mc:MovieClip,speed:Number){

          var loopMC:MovieClip=mc.createEmptyMovieClip("mc_"+1,1);

          loopMC.xinc = speed*Math.random();

          loopMC.yinc = speed*Math.random();

          loopMC.onEnterFrame=function(){

          this._parent._x += this.xinc;

          this._parent._y += this.yinc;

          // check if off-stage and call removeF(this._parent)

          }

          }

           

          function checkF(mc:MovieClip){

          var loopMC:MovieClip=mc.createEmptyMovieClip("mc_"+2,2);

          loopMC.onEnterFrame=function(){

          if(this._parent._currentframe==this._parent._totalframes){

          // perform action on this._parent, then call removeF(this._parent)

          }

          }

          }

          1 person found this helpful
          • 2. Re: assigning AS to attached movie clips
            Shad0wdrag0n Level 1

            OK, I'm not sure if I know how to get those to work properly. Where are they supposed to go; on the main timeline where the movie clips are going to be attached, or within the attached movie clips? I tried using the first function as written, placing it both on the main timeline and within the attached movie clip, but it didn't seem to work either way. It did seem to work when I tried it like:

             

            blueball.onRelease=function(){
            this.removeMovieClip();
            }

             

            Well, it sort of worked. If each attached movie has the same instance name this code only works on them one at a time. If I give each attached movie a unique instance name the code will only ever work on one of them. How do I get this to work on all of the attached movie clips at the same time?

            • 3. Re: assigning AS to attached movie clips
              Shad0wdrag0n Level 1

              Basically I want to have a movie clip get attached to the stage over and over again and each attached movie clip has a set of functions that apply only to it. Or I want a set of functions that apply to all of the attached movie clips all at the same time. Are either of those things actually possible to do in flash? Something like a skeet shoot game where each skeet gets attached to the stage and then goes flying across the screen. Each skeet gets attached and goes flying at randomly determined intervals. When you click on a skeet it explodes. Something like that.

              • 4. Re: assigning AS to attached movie clips
                kglad Adobe Community Professional & MVP

                attach those functions to frame 1 of your main timeline.  then any time you want to apply one of them to a movieclip use:

                 

                removeF(yourmovieclip);  // if this code is on the main timeline

                 

                // if you're on a movieclip's timeline whose parent is the main timeline use:

                 

                _parent.removeF(yourmovieclip);

                 

                similarly for the other functions.

                1 person found this helpful
                • 5. Re: assigning AS to attached movie clips
                  Shad0wdrag0n Level 1

                  Ah, now I get it. But how many movie clips will that code work on at the same time? Say I have 4 movie clips that get attached to the stage, one after another, and they all have the same instance name. If I have removeF(yourmovieclip); once on the same frame (where "yourmovieclip is replaced by the instance name shared by all four movie clips), will it work on all 4 movie clips? If not, then is it possible to make it work on all the movie clips at the same time? Is there a way to give each attached movie clip a unique name when it's created, and have it create the removeF(yourmovieclip); code each time a movie clip is attached where the "yourmovieclip" part is replaced by the new movie clip's unique instance name? The thing is, I don't know how many movie clips are gojing to be attached to the stage over the course of the entire movie, so I don't know how many times I should add the removeF(yourmovieclip); code. It would be nice if the removeF(yourmovieclip); code was created along with the movie clips, or have some way for the removeF(yourmovieclip); code to effect all of the attached movie clips at the same time.

                  • 6. Re: assigning AS to attached movie clips
                    kglad Adobe Community Professional & MVP

                    each function has no limit on the number of movieclips it will affect.

                    • 7. Re: assigning AS to attached movie clips
                      Shad0wdrag0n Level 1

                      But removeF(yourmovieclip); has to be added for each attached movie clip correct? yourmovieclip has to be replaced with the instance name of the movie clip it affects (so removeF(blueball1) for example)? What happens if I don't know how many movie clips are going to be attached, and their instance names are generated based on how many have already been created? Is there a way to add the removeF(yourmovieclip); code only when the movie is attached and to make it have the same instance name as the one generated for the attached movie clip?

                       

                      I don't think I'm explaining what I want too well. I've been looking for an example but so far I haven't found anything that works. Hopefully I'll find something that better shows what it is I'm trying to do.

                      • 8. Re: assigning AS to attached movie clips
                        kglad Adobe Community Professional & MVP

                        it doesn't matter whether you know the names before hand.  you'll know them when they're created and that's when you can call removeF().