Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Check if button was clicked

Explorer ,
Apr 18, 2013 Apr 18, 2013

Hello!

I have a short question to ask. Is there any way of testing if a button was clicked using as3?

I have 3 different buttons and I want to test if the right button was clicked, then the user would see the message "Correct". Otherwise, if any of the other 2 buttons were clicked, the message would be "Incorrect".

Thank you for your help!

TOPICS
ActionScript
2.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , Apr 18, 2013 Apr 18, 2013

I have solved the problem by myself !

Thanks a lot of answering, your advice was really helpful in my way to the right solution!

Translate
LEGEND ,
Apr 18, 2013 Apr 18, 2013

Assign an event listener for a MouseEvent.CLICK to the button.  Then have the event handler function for that listener deal with the message processing

btn.addEventListener(MouseEvent.CLICK, showCorrect);

function showCorrect(evt:MouseEvent):void {

   // display the correct message

}

Do similarly for the other buttons

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 18, 2013 Apr 18, 2013

I thought about that, too. But the problem is that what I am trying to make is similar to a quiz. The questions change, and each time the correct answer (button) is in a different position.

I was hoping something like this would be out there :

if (option_a is correct) {

        if ( button_a is clicked )

                    {

                         //display correct message

                        }

            else if( button_b or button_c is clicked ){

                         //display wrong message

                  }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 18, 2013 Apr 18, 2013

Yes, something like that could probably be used if the logic makes sense (to you)but you are the only one who has the inside scoop on what you want.  You need some way of determining which button is the correct one versus the incorrect ones. 

You could have all the buttons using the same event handler and check the name of the button if the name of the correct one will always be the same or if you can change the criteria to match each question.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 18, 2013 Apr 18, 2013

This is how my code looks so far if that could help you in any way.

var dividend:Array  = [6,12,24,45,36,81,18,9,16,15];

var divisor:Array  = [2,4,3,5,6,9,2,3,4,5];

var answers:Array  = [3,3,8,9,6,9,9,3,4,3];

var options:Array  = [option_a,option_b,option_c];

var temp:int = Math.floor(Math.random() * 2.99);   // random number between 0 and 2 which helps me choose a different                                                                                  option every time

var clicked:Boolean=false;

var i=1;

but0_btn.addEventListener(MouseEvent.CLICK,chosen);

but1_btn.addEventListener(MouseEvent.CLICK,chosen);

but2_btn.addEventListener(MouseEvent.CLICK,chosen);

next_btn.addEventListener(MouseEvent.CLICK, nextOperation);

function nextOperation(e:MouseEvent):void{

          num1.text=dividend;

          num2.text=divisor;

          temp= Math.floor(Math.random() * 2.99);

          if(temp==0){

             option_a.text=answers;                            //the right answer

                                        option_b.text=String(answers+1);            // wrong answer

             option_c.text=String(answers-1);            //wrong answer

                    }

                    else if(temp==1){

                                        option_b.text=answers;

             option_a.text=String(answers+1);

                                        option_c.text=String(answers-1);

                    }

                              else {

                                                  option_c.text=answers;

                                                  option_a.text=String(answers+1);

              option_b.text=String(answers-1);

                              }

          i++;

          if(i>dividend.length-1){

                 i=0;

          }

}

function chosen(e:MouseEvent):void{

                    clicked=true;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 18, 2013 Apr 18, 2013

I have solved the problem by myself !

Thanks a lot of answering, your advice was really helpful in my way to the right solution!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 18, 2013 Apr 18, 2013

You're welcome.  It always feels better when you solve things yourself.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 18, 2013 Apr 18, 2013

Yes it does!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 18, 2013 Apr 18, 2013
LATEST

You should post the solution for the benefit of someone else like yourself who might come looking for a similar solution.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 18, 2013 Apr 18, 2013

You'll be helping others to help you if you show all of the code you think you want to use for this scenario.  It will make it cklearer what you have to support it and what you want to do.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines