Copy link to clipboard
Copied
I'm creating a multiple choice quiz with multiple questions.
I have each clickable selection using "answer1", "answer2" ... as instance name.
I created standard button to show different states and make selection (like what's below):
answer1.buttonMode = true;// to show hand icon
answer1.addEventListener(MouseEvent.CLICK, answer1Click);
answer1.addEventListener(MouseEvent.ROLL_OVER, answer1Rollover);
answer1.addEventListener(MouseEvent.ROLL_OUT, answer1Rollout);
function answer1Click(ev:MouseEvent):void
{
answer1.gotoanswerndStop(2);
answer2.gotoanswerndStop(1);
answer3.gotoanswerndStop(1);
answer4.gotoanswerndStop(1);
sel = 1;
}
function answer1Rollover(ev:MouseEvent):void
{
answer1.gotoanswerndStop(2);
}
function answer1Rollout(ev:MouseEvent):void
{
if (sel != 1)
{
answer1.gotoanswerndStop(1);
}
}
I duplicate the frame for different questions but I can't reuse the same functions for the first question. It would give "Duplicate function definition." error. I don't want to rename the instance names for each new question. How can I reuse the same function for all questions?
Thanks,
This layer only for the functions don't use it to add the event listeners.. add the listeners only when the answer buttons appears:
Copy link to clipboard
Copied
Put the function in a separate layer with one key frame
Copy link to clipboard
Copied
Thanks for the help. I did that but still only works with the first question. See attachment: https://drive.google.com/open?id=0B6qhPNFLo-EpbTZFQkt5WFpMNkE
Copy link to clipboard
Copied
assign the correct answer before calling the answer listener function. eg,
var i:int;
function answerClick(ev:MouseEvent):void{
if(MovieClip(e.currentTarget).correct){
MovieClip(e.currentTarget).gotoanswerndStop(2); // assuming this is what should show if answer is correct
} else {
MovieClip(e.currentTarget).gotoanswerndStop(1);
}
}
// question 1:
answer1.correct=true;
for(i=1;i<=4;i++){
this['answer'+i].addEventListener(MouseEvent.CLICK, answerClick);
}
// question 2:
answer3.correct=true;
for(i=1;i<=4;i++){
this['answer'+i].addEventListener(MouseEvent.CLICK, answerClick);
}
// etc
Copy link to clipboard
Copied
thank you for a different approach (I like loop to make short). I will experiment and see if this works better.
Copy link to clipboard
Copied
This layer only for the functions don't use it to add the event listeners.. add the listeners only when the answer buttons appears:
Copy link to clipboard
Copied
Thank you for the help. It works now. The scripts for event listeners are identical for each question. Is there a way to simplify it? Text above the buttons is blocking the buttons. You can't click the buttons when hovering above the text. Is there way to prevent that?
Thanks,
Copy link to clipboard
Copied
Which text you mean? if it's the buttons labels then you can use "btnName.mouseChildren = false;"
Copy link to clipboard
Copied
I mean the text inside a button to make a selection. The text is not part of the button but a movie clip in a separate layer above the layer with all the buttons. It seems you can click if you hover above the text area.
Copy link to clipboard
Copied
So give an instance name to that movieClip and use: movieClipName.mouseEnabled = false;
Copy link to clipboard
Copied
That (mouseEnabled = false) works. Thanks again for fixing another problem.
Copy link to clipboard
Copied
You're welcome.
Copy link to clipboard
Copied
If it's not, use objectName.mouseEnabled = false;
Find more inspiration, events, and resources on the new Adobe Community
Explore Now