5 Replies Latest reply on Feb 5, 2013 2:10 PM by prodigalmaster

    Script to set which movie clip is clicked in the stage in flash

    enciel17

      Hi.

       

      What I am trying to do is I have several movieclips with an identifier, ex. flipbed01, flipbed02 and a button on the stage. When I click on flipbed01 movieclip I get the instance name used on the movie clip I clicked and then I put it in a variable where I can use it on another button to make it flip horizontally. And if I click flipbed02 movieclip then click the button, flipbed02 movieclip gets flipped horizontally

       

      Can anyone help me on this?

        • 1. Re: Script to set which movie clip is clicked in the stage in flash
          prodigalmaster Level 2

          To flip a movieclip horizontally use:

           

          flipbed01._xscale = -100;

           

          I am a little confused by your paragraph but I'll see what I can do.

           

          So, when you press the button, it flips the last movieclip you click?

           

          I recommend you make the button a movieclip too and make the over/out/down frames and actionscript manually, if you wish.

           

          If there are only 2 movieclips, flipbed01 and flipbed02 then you could use a boolean variable like:

           

          var blnFlipbed01_clicked:Boolean;

           

          Then:

           

          flipbed01.onPress = function(){

               blnFlipbed01_clicked = true;

          };

          flipbed02.onPress = function(){

               blnFlipbed01_clicked = false;

          };

          buttonMC.onPress = function(){

               If(blnFlipbed01_clicked){

                    flipbed01._xscale = -100;

               } else {

                    flipbed02._xscale = -100;

               }

          };

           

          Remember to keep all your code on the main timeline.

          • 2. Re: Script to set which movie clip is clicked in the stage in flash
            enciel17 Level 1

            Actually I have lots of movieclips not just two, what I hoped I could do was for example I have 10 movieclips then I have this only one button. And then all these 10 movieclips are on stage, when I click 01 movie clip, then I click this button, it will flip the 01 movieclip, but when I click 02 movieclip, then I also click that same button movieclip02 will flip. and so on

             

            Very grateful for your help! sorry if my original question was confusing, hope it is clearer this time.

            • 3. Re: Script to set which movie clip is clicked in the stage in flash
              prodigalmaster Level 2

              Ok then instead of a variable which can hold 2 values, use the 'Number' data type.

               

              var numFlipbed_LastClicked:Number;

               

              Then to apply a on press function to all of them use a for loop. Make sure you make the suffix (last bit) of the identifiers a unique number (not 01, 02 to 10 etc. but 1, 2 to 10; flipbed1, flipbed2 and so on, to allow the following code to work).

               

              var totalMCs:Number = 10; //how many movieclips

              //For i from 0 to 10 (executes code within 10 times and increments i by 1 each time)

              for(i=0; i<totalMCs; i++){

                   _root["flipbed"+i].onPress = function(){

                        //set the last movieclip clicked id

                        numFlipbed_LastClick = i;

                   };

                   buttonMC.onPress = function(){

                        //flip the movieclip of the last clicked movieclip

                        _root["flipbed"+numFlipbed_LastClick]._xscale = -100;

                   };

              }


              • 4. Re: Script to set which movie clip is clicked in the stage in flash
                enciel17 Level 1

                Hi,

                 

                I tried that code and changed var totalMCs:Number = 2;  for testing purposes only if it would work but what happens is when I click flipbed2 and then click the buttonMC - nothing happens, and when I click flipbed1 movieclip and then click buttonMC - flipbed2 is the one that flips instead of flipbed1.

                 

                Why is that? And another problem I am encountering is that I have to set an instance name to make the code work right? Currently my movieclips are set to be exported in actionscript in frame1 (so that I can call them using attachMovie), so that I can call them on stage when I want. So right now they don't have any instance. Is there anyway to make this work using this code?

                 

                 


                • 5. Re: Script to set which movie clip is clicked in the stage in flash
                  prodigalmaster Level 2

                  Did you declare:

                  var blnFlipbed01_clicked:Boolean;

                  ?

                   

                  If you are attaching the movieclips from the library, the second parameter sets the new identifier. You could use: "flipbed"+i with it inside the array, meaning using attachMovieclip as one line, and it attaches flipbed every time it loops (totalMCs) and each flipbed has a unique identifier (flipbed0, flipbed1) - remember, the loop begins with 0, not 1.

                   

                  The third parameter of attachMovie sets the depth. It can be very hard to work with, but youcould try using: this.getNextHighestDepth() or you could use: i, as the depth - but ensure no other movieclip depths will be 0, 1.. etc. To get around this, I made up a way a whileago where you make a varaible like this: var depthFlipbeds:Number = 2000; Then make the depth depthFlipbeds+i and for each set of movieclips you create, make a new starting depth like: 4000 etc. Otherwise, the depths overlap and the movieclips disappear.