views:

18

answers:

1

I'm going to make a counter that will count the correct answers of an exam questions. The counter will collect only the correct answers from each frame.

Here is the first frame:

var counter:Number = 0;
var correctAns:Number;

correctAns = ans1_mc.alpha;

function clicked1(event:MouseEvent):void
{
ans1_mc.alpha = 1;
ans2_mc.alpha = 0;
ans3_mc.alpha = 0;
ans4_mc.alpha = 0;
}
function clicked2(event:MouseEvent):void
{
ans1_mc.alpha = 0;
ans2_mc.alpha = 1;
ans3_mc.alpha = 0;
ans4_mc.alpha = 0;
}
function clicked3(event:MouseEvent):void
{
ans1_mc.alpha = 0;
ans2_mc.alpha = 0;
ans3_mc.alpha = 1;
ans4_mc.alpha = 0;
}
function clicked4(event:MouseEvent):void
{
ans1_mc.alpha = 0;
ans2_mc.alpha = 0;
ans3_mc.alpha = 0;
ans4_mc.alpha = 1;
}
function submit(event:MouseEvent):void
{   
if (correctAns == 1)
{
    counter++;
}
else
{
    counter = counter;
}
trace (counter);
gotoAndStop(currentFrame + 1);
}


ans1_btn.addEventListener(MouseEvent.CLICK, clicked1);
ans2_btn.addEventListener(MouseEvent.CLICK, clicked2);
ans3_btn.addEventListener(MouseEvent.CLICK, clicked3);
ans4_btn.addEventListener(MouseEvent.CLICK, clicked4);

submit_btn.addEventListener(MouseEvent.CLICK, submit);


ans1_mc.alpha = 0;
ans2_mc.alpha = 0;
ans3_mc.alpha = 0;
ans4_mc.alpha = 0;

text1_txt.text = "A";
text2_txt.text = "B";
text3_txt.text = "C";
text4_txt.text = "D";

stop();

The Second Frame is:

ans1_mc.alpha = 0; ans2_mc.alpha = 0; ans3_mc.alpha = 0; ans4_mc.alpha = 0;

correctAns = ans1_mc.alpha;

text1_txt.text = "E";
text2_txt.text = "F";
text3_txt.text = "G";
text4_txt.text = "H";

stop();

All other frames will be as the second frame.

I want to know where is the wrong.

A: 

Your general approach is wrong here & your game is not very scalable. You need to take an overview of your game and try to come up with a better logic.

You can identify a button in your listener so there's no need to add so many event listeners, one would be fine. Identify the button that was clicked and set the answer accordingly.

 function clickHandler(event:MouseEvent):void
 {
      trace( event.currentTarget);
  }

If you put all your buttons in an Array, it'll be easy to set their alpha property.

 var buttons:Array = [ans1_mc, ans2_mc, ans3_mc , ans4_mc];

 function clickHandler(event:MouseEvent):void
 {
    //set all buttons alpha to 0
    for each( var button:MovieClip in buttons )
       button.alpha = 0;

     //set the alpha of the clicked button to 1
     event.currentTarget.alpha = 1;

     //set the correct answer
     if( event.currentTarget == ans1_mc )
        correctAns = 1;
  }

I haven't tested your code so I'm not sure how mistakes there are, in any case , you should set the correctAns variable in the function, like I did above.

PatrickS
Thanks a lot. I will try your code and will be back to give you a feedback.
Maged Yaseen
In general, this is not what I need actualy, but that does not mean you didn't help me, you did a good work that helped me to use a good method to reach my goal. Thanks a lot
Maged Yaseen