Skip navigation
Currently Being Moderated

Detecting if a button is clicked

Apr 8, 2013 8:00 PM

Tags: #action #actionscript #as3.0 #movieclip #cs6 #actionscript_3

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

 
Replies
  • Currently Being Moderated
    Apr 8, 2013 11:21 PM   in reply to EverestJ

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

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 9, 2013 7:42 AM   in reply to EverestJ

    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 = [];

    }

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 9, 2013 7:24 AM   in reply to EverestJ

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 9, 2013 8:16 AM   in reply to EverestJ

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

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 10, 2013 1:29 AM   in reply to EverestJ

    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

    }

    ...

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 10, 2013 1:16 PM   in reply to EverestJ

    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

     

    }

     


     
    |
    Mark as:
  • Currently Being Moderated
    Apr 16, 2013 11:57 PM   in reply to EverestJ

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

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points