14 Replies Latest reply: Apr 17, 2013 12:03 AM by EverestJ RSS

    Detecting if a button is clicked

    EverestJ

      I am trying to make it so if you click the 5 buttons on my main page frame it will advance you to another frame.  The problem I am having is that the buttons that you click take you away from the main page frame.  So say the main page frame is on frame 1.  buttons 1-5 take you t frames 2-6 (within the main movie frame).  I would like to be able to send the user to page 7 once the 5 buttons are clicked, but I am running into problems with detecting if the button was clicked.  Here is my code with "main" being the movieclip that everything else is inside. 

       

      var clickCount:int = 0;

       

      var clipArray:Array = [main.btn1_mc, main.btn2_mc, main.btn3_mc, main.btn4_mc, main.btn5_mc];

      for (var i:int = 0; i < clipArray.length; i++) {

          clipArray[i].buttonMode = true;

          clipArray[i].addEventListener(MouseEvent.CLICK, clickHandler);

          clipArray[i].isClicked = false;

      }

          function clickHandler(event:MouseEvent):void {

              switch (event.currentTarget) {

                  case main.btn1_mc :

                      trace("btn1");

                       break;

                  case main.btn2_mc :

                      trace("btn2");

                      break;

                  case main.btn3_mc :

                      trace("btn3");

                      break;

                  case main.btn4_mc :

                      trace("btn4");

                      break;

                  case main.btn5_mc :

                      trace("btn5");

                      break;

              }

               clickCount++;

               trace(clickCount);

          if(event.currentTarget.isClicked == false){

         

           event.currentTarget.isClicked = true;

          }

         

          if(clickCount == clipArray.length){

              trace("All buttons have been clicked");

             

          }

      }

      stop();

       

      any help is appreciated as always.  Thanks

        • 1. Re: Detecting if a button is clicked
          moccamaximum Community Member

          Flash doesn`t have "pages". Are you refering to scenes? Show a screenshot of the timeline of your main MovieClip.

          • 2. Re: Detecting if a button is clicked
            FlashGodThe2nd Community Member

            You can use a second array that contains the buttons that were clicked.

             

            // initialize

            var buttonsClicked:Array = [];

            ...

             

            // look if the button was collected as clicked

            if (buttonsClicked.indexOf(event.currentTarget) == -1){

                 // if not found in array add it

                 buttonsClicked.push(event.currentTarget);

            }

             

            ...

            // check if all buttons were clicked

            if (buttonsClicked.length == clipArray.length){

                 // all buttons were clicked, let's party

                 // or go frame 7 or whatever

             

                 // clear the clicked array again for next turn...

                 buttonsClicked = [];

            }

            • 3. Re: Detecting if a button is clicked
              EverestJ Community Member

              I am not referring to scenes.  All the buttons are contained within one movie clip.  on the movie clip timeline, the 5 buttons are in frame one.  button one moves the movieclip timeline forward to frame two.  then a button in frame two brings you back to frame one (all in the movie clip timeline).  button 2 takes you to frame three in the movieclip timeline and a button in frame 3 brings you back to frame one and so forth.  I want my code to detect if the buttons have been clicked, so when all buttons are clicked, it moves to another frame inside the movie clip.  I know this sounds confusing and is hard to explain.  My code now only detects when the first button is clicked and that's it.  Any help is much appreciated.  Thanks

              • 4. Re: Detecting if a button is clicked
                moccamaximum Community Member

                I know this sounds confusing and is hard to explain.

                Agreed. This why it would be helpful to put a screenshot of the timeline that shows: all 5 buttons, the stage, if you have actionscript on the timeline a n example how you use it.

                • 5. Re: Detecting if a button is clicked
                  EverestJ Community Member

                   

                  this is simplified for forum purposes...

                  above is the main move clip.  the five buttons are inside the main movie clip

                   

                   

                  above is inside the main movie clip

                   

                  when you click on btn1 it takes you to this:

                   

                  when you click back it goes back to :

                   

                   

                   

                  if btn2 is clicked :

                   

                   

                  and so forth.

                   

                  I want to make it so once all 5 btns are clicked it will take you to:

                   

                  which is frame 7 inside the main movie clip.

                   

                  Hope this helps clear things up.  I am not even sure if this is possible.  Any help is appreciated as always.

                  Thanks!

                  • 6. Re: Detecting if a button is clicked
                    moccamaximum Community Member

                    I can`t see any pictures. Make sure you use jpg/png for compression.

                    • 7. Re: Detecting if a button is clicked
                      EverestJ Community Member

                      i noticed that... it will not let me insert an image.  i keep getting "error see below" and below it just says "null" 

                      my files are within the 2mb size and i even scaled them.  i don't know what is going on

                      • 8. Re: Detecting if a button is clicked
                        EverestJ Community Member

                        screenshot1.png

                        this is simplified for forum purposes...

                        above is the main move clip.  the five buttons are inside the main movie clip

                         

                        screenshot2.png

                        above is inside the main movie clip

                         

                        when you click on btn1 it takes you to this:

                        screenshot3.png

                         

                        when you click back it goes back to :

                        screenshot2.png

                         

                         

                         

                        if btn2 is clicked :screenshot4.jpg

                         

                         

                        and so forth.

                         

                        I want to make it so once all 5 btns are clicked it will take you to:

                         

                        screenshot5.png

                         

                        which is frame 7 inside the main movie clip.

                         

                        Hope this helps clear things up.  I am not even sure if this is possible.  Any help is appreciated as always.

                        Thanks!

                        • 9. Re: Detecting if a button is clicked
                          moccamaximum Community Member

                          make a var on the highest level (root) of your fla:

                           

                          var clickcounter:int = 0;

                           

                          and a function

                           

                          function getClicks():Boolean{

                              if(clickcounter>=5){

                                return true;

                               }

                             else{

                             return false;

                             }

                          }

                           

                          then inside of every MouseEvent.CLICK-handler of your various buttons add these lines:

                          ...

                          MovieClip(root).clickcounter ++;

                          if(MovieClip(root).getClicks()){

                             MovieClip(root).main.gotoAndStop(7);

                          }

                          else{

                              //put here the stuff you want to be executed regularly

                          }

                          ...

                          • 10. Re: Detecting if a button is clicked
                            EverestJ Community Member

                            This is awesome.  Thank you.  One last question, how can I make it so once the button is clicked, it does not add to the counter?  Otherwise, I can click the same button  times to advance.  Thanks again, you have been great and really helped my school project along.

                            • 11. Re: Detecting if a button is clicked
                              moccamaximum Community Member

                              add the following line on the highest level of your fla:

                               

                              var clickOrigins:Array = [];

                              function checkArray(_target:String,_arr:Array):Boolean{

                                   if(_arr.indexOf(_target)==-1){

                                       _arr.push(_target);

                                      return true;

                                   }

                                   else{

                                     trace("you already clicked that");

                                     return false;

                                   }

                              }

                               

                              and change the addition to your clickHandler to

                              //event has to have the same syntax as the argument of your ClickHandler, so if you have written function clickHandler(evt:MouseEvent)...event must be called evt

                              if ((MovieClip(root).checkArray(MovieClip(root).clickOrigins, event.currentTarget.name)))

                              {

                               

                                  MovieClip(root).clickcounter++;

                               

                              }

                               

                              if (MovieClip(root).getClicks())

                              {

                               

                                  MovieClip(root).main.gotoAndStop(7);

                                 //empty the array if you must use it again:

                                 //MovieClip(root).clickOrigins = [];

                               

                              }

                               

                              else

                              {

                               

                                      //put here the stuff you want to be executed regularly

                               

                              }

                               


                              • 12. Re: Detecting if a button is clicked
                                EverestJ Community Member

                                So I went in and tried the code, and when I click it throws this error:

                                 

                                TypeError: Error #1009: Cannot access a property or method of a null object reference.

                                    at fl.video::UIManager/http://www.adobe.com/2007/flash/flvplayback/internal::hookUpCustomComponents()

                                    at fl.video::FLVPlayback/http://www.adobe.com/2007/flash/flvplayback/internal::handleVideoEvent()

                                    at flash.events::EventDispatcher/dispatchEventFunction()

                                    at flash.events::EventDispatcher/dispatchEvent()

                                    at fl.video::VideoPlayer/http://www.adobe.com/2007/flash/flvplayback/internal::setState()

                                    at fl.video::VideoPlayer/http://www.adobe.com/2007/flash/flvplayback/internal::finishAutoResize()

                                    at flash.utils::Timer/_timerDispatch()

                                    at flash.utils::Timer/tick()

                                 

                                 

                                I am not sure what is going on with it.  Any help would be much appreciated.  Thanks

                                • 13. Re: Detecting if a button is clicked
                                  moccamaximum Community Member

                                  You don`t show where or how you use a videoplayer in your project or a Timer

                                  • 14. Re: Detecting if a button is clicked
                                    EverestJ Community Member

                                    I figured it out.  Just a bonehead mistake on my part.  Thanks you are awesome!