8 Replies Latest reply on Jun 28, 2013 2:10 PM by kglad

    To use the same code for many movieclips

    a.bisewski Level 1

      Dear all.

       

      I am using the flash to build my personal book online. I need zoom  many movieclip, my images.

       

      i make the code and function perfectly. But I want to find a way to use the same function for all movieclips and so, mantaining my code clean.

       

      mc_01.addEventListener(MouseEvent.MIDDLE_MOUSE_DOWN, Drag);

      mc_01.addEventListener(MouseEvent.MIDDLE_MOUSE_UP, unDrag);

      mc_02.addEventListener(MouseEvent.MIDDLE_MOUSE_DOWN, Drag);

      mc_02.addEventListener(MouseEvent.MIDDLE_MOUSE_UP, unDrag);

      and many others....

       

      function Zoom(e:MouseEvent):void

              {

              mc_01.x = 0;  //I try here for this.x but the previews movie, called "Books" is who have changed your x value.

              mc_01.y = 0;

       

              }

      I

       

      EDIT: For untangle, the previews movie is called MyAllPhotos and then, contain all movieclips that are each movie clip is one my images

        • 1. Re: To use the same code for many movieclips
          kglad Adobe Community Professional & MVP

          use:

           

          var n:int = ?; //number of mc_xx movieclips

           

          for(var i:int=1;i<n;i++){

          this["mc_"+padF(i)].addEventListener(MouseEvent.MIDDLE_MOUSE_DOWN, Drag);

          this["mc_"+padF(i)].addEventListener(MouseEvent.MIDDLE_MOUSE_UP, unDrag);

          }

           

          function padF(i:int):String{

          var s:String=i.toString();

          while(s.length<2){

          s="0"+s;

          }

          return s;

          }

          1 person found this helpful
          • 2. Re: To use the same code for many movieclips
            a.bisewski Level 1

            Hi Mr Kglad.

             

            And when I call a property like a X position I use this["mc_"+padF(i)].x = 0;  for substitute the mc_01.x = 0 ?

             

            My problem realy is inside the function. Your loop add an event to my clips and direction to function Drag and UnDrag.

             

            But inside in this functions, I need get some informations for the clip.

             

            Here the code:

            function Zoom(e:MouseEvent):void  //Sorry, but the correct name my functions is Zoom and not Drag and unDrag

                    {

             

                       var mod:Number = 50;

                       mc_01.scaleX += e.delta / mod; //in this name mc what I need to use? this, parent? I try and not function.

                       mc_01.scaleY += e.delta / mod;

             

                   }


            • 3. Re: To use the same code for many movieclips
              a.bisewski Level 1

              e.target.name  appear to function, I test with trace and I get the correct name of my movieclip.

               

              EDIT: Strange, function only for the firt movie clip...including the trace...

              • 4. Re: To use the same code for many movieclips
                a.bisewski Level 1

                001.jpg

                Here my config. See that only the first movie function because my code is in the same frame. I dont undertanding this because the code is in all sequence frames.

                 

                If i change the location code to other frame, so the movie in the new frame function...

                • 5. Re: To use the same code for many movieclips
                  kglad Adobe Community Professional & MVP

                  use the event's currentTarget property to gain a reference to the movieclip with the listener:

                   

                  function zoom(e:MouseEvent):void{

                  var mc:MovieClip=MovieClip(e.currentTarget);

                  mc.x =

                  mc.y=

                  etc

                  }

                  1 person found this helpful
                  • 6. Re: To use the same code for many movieclips
                    a.bisewski Level 1

                    Thanks Klad, work in any movie but I dont know How to do the function to function in all frames. Like in image, only the movie that are in the same frame function perfectly, the other movies, nothing happens.

                    • 7. Re: To use the same code for many movieclips
                      a.bisewski Level 1

                      The unique way that I get to function was creating differents function for each frame.

                       

                      I dont undertanding the scope that flash use.

                       

                      Like the image above, if I select the frame 5, the code that is in frame 2 also showed in framee 5, because not have many frame, and only one single with code.

                       

                      But If I can that the movieclip in frame 5 function, I need to change the possition code for 2 frame to the 5, and work ok, but now, the movie clip in frame 2 not work more...

                       

                      sorry my bad english....

                      • 8. Re: To use the same code for many movieclips
                        kglad Adobe Community Professional & MVP

                        those movieclips must exist when that code executes.  if, at same time after that, a movieclip no longer exists (because of a frame change, for example), the code is no longer valid.

                         

                        ie, the movieclips must continue to exist.  they do not need to be visible.  ie, they could have their visible property assigned to false or they could be removed from the display list (removeChild).