8 Replies Latest reply on Sep 8, 2009 11:08 AM by Rothrock

    Count click on movieclip. Then disable clip?

    okkarne Level 1

      Hi

      I have a little problem with counter for movieclips. (Its a making icecream application.)

       

      This particular scene I want the user to use all 3 Electric mixers before moving on to the next scene.

       

      I have done it now so that the user must klick on mixer at least 3 times. But it can be the same clip.

       

      The problem I have is to single out each clip. So that if the clip have been clicked once. The counter for that clip is disabled.

       

      Hope you can understand what I mean.

       

      Code:

      myVar=0;

       

      function dragSetup(clip, targ) {

      clip.onPress = function() {

      startDrag(this, false, clip._x, 10, clip._x, 180);

      this.beingDragged=true;

      myVar++

      if (eval(this._droptarget) == targ) {

      this.onTarget = true;

      }

      };

       

      clip.onRelease = function () {

      stopDrag();

      this.beingDragged=false;

      if (eval(this._droptarget) == targ) {

      this.onTarget = true;

      } else {

      this.onTarget = false;

      }

      };

       

      dragSetup(visp1,dropzone);

      dragSetup(visp2,dropzone2);

      dragSetup(visp3,dropzone3)

       

      Then in button to move to next scene

       

      on (press) {

      if (myVar<=2){

      _root.maste2._alpha=100; /* displays warning to use mixer */

      }

      else{

      gotoAndStop(4);

      }

      }

       

      So any help would be appreciated.

       

      Regards

      olle

        • 1. Re: Count click on movieclip. Then disable clip?
          kglad Adobe Community Professional & MVP

          create an array, add each clicked movieclip to the array, if it's not already in the array, and check the array's length each time a button is clicked.  when the length is 3, move to the next scene.

          • 2. Re: Count click on movieclip. Then disable clip?
            okkarne Level 1

            Hi

            Thank you for answering.

            But as you may have guessed im not so experienced.

            So I don't know how to do what you described.

             

            I can probably do the last part to check when the length is 3 and move on.

             

            But not to add movieclips names to the array when they are clicked.

             

            If you please have a small example to get me started.

             

            Regards

            Olle

            • 3. Re: Count click on movieclip. Then disable clip?
              Ned Murphy Adobe Community Professional & MVP

              To add items to an array you would use the push() method, as in: myArray.push(mc); .  If you look up the Array class in the help documents you will find all of the various properties and methods associated with an array explained.

              • 4. Re: Count click on movieclip. Then disable clip?
                kglad Adobe Community Professional & MVP

                var btnClickedA:Array = [];

                 

                btn1.onRelease=function(){

                checkComplete(this);

                }

                btn2.onRelease=function(){

                checkComplete(this);

                }

                btn3.onRelease=function(){

                checkComplete(this);

                }

                 

                function checkComplete(mc:MovieClip){

                if(!memberOf(btnClickedA,mc)){

                btnClickedA.push(mc);

                if(btnClickedA.length==3){

                // you're done, go to whatever

                }

                }

                }

                 

                function memberOf(a:Array,e:MovieClip):Boolean{

                for(var i:Number=0;i<a.length;i++){

                if(a[i]==e){

                return true;

                }

                }

                return false;

                }

                • 5. Re: Count click on movieclip. Then disable clip?
                  Rothrock Level 5

                  I like to do this with a binary counter. It is very expandable and easy -- once you get the idea.

                   

                  Basically each clip gets a "place" in a binary number and all of the places have to be switched from zero to 1. In so many ways learning to count from zero is helpful with programming in flash, so I would have my buttons start with the name btn0, btn1, etc....

                   

                   

                  var numBlenders:Number=3;

                  var visited:Number=0;

                  var needToVisit:Number=Math.pow(2,numBlenders)-1;

                   

                  for(var i=0;i<numBlenders;i++){

                  var curBtn=this["btn"+i];

                  curBtn.ID=i;

                  curBtn.onRelease=checkComplete;

                  }

                   

                  function checkComplete(){

                   

                       visited |= 1<<this.ID;

                   

                       if(visited == needToVisit){

                            trace("All visited");

                       } else {

                            trace("Not all visited");

                       }

                  }

                   

                  The ease of this is that it is easy to adjust if you need more blenders just add more to the stage (with the correct name) and change the numBlenders variable.

                   

                  There is no control here for which order they turn them on. Additionally I haven't done anything to disable the button when it is clicked but that would be pretty easy to add. Or if they can turn them on/off you should replace the first line in checkComplete with this:

                   

                  visite ^= 1<<this.ID;

                   

                  The first way just makes sure that they have clicked on it at some point. The second way works more like an on/off switch where they have to have them all on at the same time.

                  • 6. Re: Count click on movieclip. Then disable clip?
                    kglad Adobe Community Professional & MVP

                    that's interesting.

                    • 7. Re: Count click on movieclip. Then disable clip?
                      okkarne Level 1

                      Well, thank you all for the input and examples!

                       

                      I have learned a lot from it.

                       

                      Now I have to play around with the different options for a while

                       

                      Acctually I found another way tha was quite ok.

                      http://proto.layer51.com/d.aspx?f=613

                       

                      Because it was so easy to add the mc name  to the array with the push() method.

                      I needed a way to check if the name was already there. And that was what Array.prototype.pushDistinct did.

                       

                      The sad part with my try with Array.prototype.pushDistinct was that it did not function with Fuse wich I used for some tweening.

                       

                      So again, thank you for all the help!

                       

                      Regards

                      olle

                      • 8. Re: Count click on movieclip. Then disable clip?
                        Rothrock Level 5

                        Be very careful when adding things to the Array.prototype. If you do a for...in loop over an array after doing that you might find that you have an extra item in that loop. The new method/function will show up as an item in the array and that can break a lot of things. That could be part of the problem with the Fuse. It most likely uses for...in loops within its code.

                         

                        If you do use that approach be sure that there is an ASSetPropFlag that will hide the new method from (as I like to say) the tyranny of the for...in loop. That would look something like:

                         

                        ASSetPropFlags(Array.prototype, ["pushDistinct"], 1, 1);

                         

                        But I've got to say I really like my technique.

                         

                        It is easy to expand to a lot of different uses and can easily expand up to 32 items -- I've only ever used it for up to 10 or so. And the nice way it switches from the or assignment to the xor assignment and provides a nice functionality for a lot of purposes.