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

how to share the same function from different frames

Contributor ,
Nov 10, 2015 Nov 10, 2015

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,

TOPICS
ActionScript
1.2K
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

Enthusiast , Nov 10, 2015 Nov 10, 2015

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

Translate
Enthusiast ,
Nov 10, 2015 Nov 10, 2015

Put the function in a separate layer with one key frame

Screen Shot 2015-11-10 at 10.22.50 PM.png

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
Contributor ,
Nov 10, 2015 Nov 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

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
Community Expert ,
Nov 10, 2015 Nov 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

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
Contributor ,
Nov 11, 2015 Nov 11, 2015

thank you for a different approach (I like loop to make short). I will experiment and see if this works better.

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
Enthusiast ,
Nov 10, 2015 Nov 10, 2015

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

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
Contributor ,
Nov 11, 2015 Nov 11, 2015

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,

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
Enthusiast ,
Nov 11, 2015 Nov 11, 2015

Which text you mean? if it's the buttons labels then you can use "btnName.mouseChildren = false;"

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
Contributor ,
Nov 11, 2015 Nov 11, 2015

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.

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
Enthusiast ,
Nov 11, 2015 Nov 11, 2015

So give an instance name to that movieClip and use: movieClipName.mouseEnabled = false;

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
Contributor ,
Nov 11, 2015 Nov 11, 2015

That (mouseEnabled = false) works. Thanks again for fixing another problem.

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
Enthusiast ,
Nov 11, 2015 Nov 11, 2015
LATEST

You're welcome.

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
Enthusiast ,
Nov 11, 2015 Nov 11, 2015

If it's not, use objectName.mouseEnabled = false;

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