Skip to main content
Inspiring
April 9, 2013
Answered

Detecting if a button is clicked

  • April 9, 2013
  • 2 replies
  • 11431 views

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.buttonMode = true;

    clipArray.addEventListener(MouseEvent.CLICK, clickHandler);

    clipArray.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

This topic has been closed for replies.
Correct answer moccamaximum

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.


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

}


2 replies

April 9, 2013

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

}

Inspiring
April 9, 2013

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

EverestJAuthor
Inspiring
April 9, 2013

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

Inspiring
April 9, 2013

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.