Skip to main content
wfzen
Inspiring
November 10, 2015
Answered

how to share the same function from different frames

  • November 10, 2015
  • 1 reply
  • 1270 views

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 topic has been closed for replies.
Correct answer nezarov

This layer only for the functions don't use it to add the event listeners.. add the listeners only when the answer buttons appears:

1 reply

Inspiring
November 10, 2015

Put the function in a separate layer with one key frame

wfzen
wfzenAuthor
Inspiring
November 10, 2015

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

kglad
Community Expert
Community Expert
November 10, 2015

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