Copy link to clipboard
Copied
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!
I have solved the problem by myself
!
Thanks a lot of answering, your advice was really helpful in my way to the right solution!
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
}
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
I have solved the problem by myself
!
Thanks a lot of answering, your advice was really helpful in my way to the right solution!
Copy link to clipboard
Copied
You're welcome. It always feels better when you solve things yourself.
Copy link to clipboard
Copied
Yes it does!![]()
Copy link to clipboard
Copied
You should post the solution for the benefit of someone else like yourself who might come looking for a similar solution.
Copy link to clipboard
Copied
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.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more