11 Replies Latest reply on Aug 30, 2012 11:59 AM by kglad

    Remove duplicate movieclips problem

    learner102

      I have a shooter game where i duplicate bullet holes in the wall.  Works nicely though i have to use Version: Flash Player 6 for the code to work.  Here is the simple code:

       

      hit1.onPress=function(){

           i++;

           _root.bullet.duplicateMovieClip("bulletNew", i);

      }

       

      I have a Reset Button that i would like to use to remove all the duplicate "bulletNew" movieclips at once.  This is what i have so far but it only removes one duplicate at a time:

       

      reset_mc.onRelease=function(){

           removeMovieClip("bulletNew");

      }

       

      Please be clear with instructions because I have never grasped AS too well.

       

      Thanks.

        • 1. Re: Remove duplicate movieclips problem
          kglad Adobe Community Professional & MVP

          to publish for later fp versions, initialize your variables (like i) and check upper/lower case of all code.

           

           

          var i:Number=0;
          var tl:MovieClip=this;

           

          hit1.onPress=function(){

               i++;

               _root.bullet.duplicateMovieClip("bulletNew"+i, i);

          }

           

           

           

          reset_mc.onRelease=function(){

             for(var j:Number=0;j<i;j++){

          if(tl["bulletNew"+i]){

          tl["bulletNew"+i].removeMovieClip();

          }

          }

          }

           

           

           

           

          p.s.  if that's the only place you create bullet holes, you can make that code more efficient by only looping through movieclips that exist.  for example:

           

          var i:Number=0;

          var bulletNewA:Array=[];

          hit1.onPress=function(){

          bulletNewA.push(_root.bullet.duplicateMovieClip("whatever",i));

          }

          reset_mc.onRelease=function(){

          for(var j:Number=bulletNewA.length-1;j>=0;j--){

          var mc:MovieClip=bulletNewA.splice(j,1)[0];

          mc.removeMovieClip();

          }

          }

          • 2. Re: Remove duplicate movieclips problem
            learner102 Level 1

            I attached your code but it only removes the last mc created.  So I don't know what's going on.

            • 3. Re: Remove duplicate movieclips problem
              kglad Adobe Community Professional & MVP

              copy and paste the code you're using.

              • 4. Re: Remove duplicate movieclips problem
                learner102 Level 1

                Adjusted code to duplicate bullet holes when clicking bulletHit_mc:

                 

                var i:Number=0;
                var tl:MovieClip=this;

                Mouse.hide();
                startDrag(target_mc,true);

                bulletHit_mc.tabEnabled=false;
                bulletHit_mc.onPress=function(){
                i++;
                _root.bullet.duplicateMovieClip("bulletNew"+i, i);
                spark_mc.gotoAndPlay(2);

                }

                 

                 

                Adjusted code for reset movieclip (to remove all duplicated mcs):

                 

                reset_mc.tabEnabled=false;
                reset_mc.onRelease=function(){
                   
                for(var j:Number=0;j<i;j++){

                if(tl["bulletNew"+i]){

                tl["bulletNew"+i].removeMovieClip();

                }

                }

                }

                 

                Its pretty much what you have given me but only removes one of the duplicated mcs

                 

                Thanks for looking.

                • 5. Re: Remove duplicate movieclips problem
                  kglad Adobe Community Professional & MVP

                  oops, my fault.  use:

                   

                   

                   

                  var i:Number=0;
                  var tl:MovieClip=_root.bullet;

                  Mouse.hide();
                  startDrag(target_mc,true);

                  bulletHit_mc.tabEnabled=false;
                  bulletHit_mc.onPress=function(){
                  i++;
                  _root.bullet.duplicateMovieClip("bulletNew"+i, i);
                  spark_mc.gotoAndPlay(2);

                  }

                   

                   

                  reset_mc.tabEnabled=false;
                  reset_mc.onRelease=function(){
                  for(var j:Number=0;j<i;j++){

                  if(tl["bulletNew"+j]){

                  tl["bulletNew"+j].removeMovieClip();

                  }

                  }

                  }

                   

                  • 6. Re: Remove duplicate movieclips problem
                    learner102 Level 1

                    Well, I'm sorry to say it had no effect on any of the duplicated MCs this time.  Is there any other information you need from me to attempt another try?  I appreciate your time looking into this.

                    • 7. Re: Remove duplicate movieclips problem
                      kglad Adobe Community Professional & MVP

                      one more try:

                       

                       

                       

                       

                       

                      var i:Number=0;
                      var tl:MovieClip=_root;

                      Mouse.hide();
                      startDrag(target_mc,true);

                      bulletHit_mc.tabEnabled=false;
                      bulletHit_mc.onPress=function(){
                      i++;
                      _root.bullet.duplicateMovieClip("bulletNew"+i, i);
                      spark_mc.gotoAndPlay(2);

                      }

                       

                       

                      reset_mc.tabEnabled=false;
                      reset_mc.onRelease=function(){
                      for(var j:Number=0;j<i;j++){

                      if(tl["bulletNew"+j]){

                      tl["bulletNew"+j].removeMovieClip();

                      }

                      }

                      }

                       

                      • 8. Re: Remove duplicate movieclips problem
                        learner102 Level 1

                        I tried re-inserting the code thismorning and it appears to have WORKED!  But it still leaves behind the very last duplicate created.  Its not a big deal but if you could help me with this last hurdle....Thanks again for your time!

                        • 9. Re: Remove duplicate movieclips problem
                          kglad Adobe Community Professional & MVP

                          use:

                           

                           

                           

                           

                           

                          var i:Number=0;
                          var tl:MovieClip=_root;

                          Mouse.hide();
                          startDrag(target_mc,true);

                          bulletHit_mc.tabEnabled=false;
                          bulletHit_mc.onPress=function(){
                          i++;
                          _root.bullet.duplicateMovieClip("bulletNew"+i, i);
                          spark_mc.gotoAndPlay(2);

                          }

                           

                           

                          reset_mc.tabEnabled=false;
                          reset_mc.onRelease=function(){
                          for(var j:Number=0;j<=i;j++){

                          if(tl["bulletNew"+j]){

                          tl["bulletNew"+j].removeMovieClip();

                          }

                          }

                          }

                           

                          p.s.  please mark helpful/correct responses.

                          • 10. Re: Remove duplicate movieclips problem
                            learner102 Level 1

                            Thank you so much, kglad.  You are awesome!  Marked as correct!  I may ask one more question related to this later if i can't figure it out myself.

                            • 11. Re: Remove duplicate movieclips problem
                              kglad Adobe Community Professional & MVP

                              you're welcome.