A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Counter

  1. #1
    Member
    Join Date
    Sep 2002
    Posts
    30
    I am trying to create a 10 question quiz.
    After you put an answer in, then click a button, I put the actionscript as below, but beofre looking at that, I guess I should explain what I trying to do.

    That button checks whether the answer is correct or not,
    if correct it will play another instance to frame"happy", else, it plays to frame "wrong".

    At the end of these instance timeline, there is a script that generates a new set of question.

    **Now what the problem is that I wish to have a counter on the button, so that after 10 clicks, it will jump to a new frame and say something like level complete. But it seems that no better what, after just one click it goes straight to the "finish" frame.


    on (release) {
    totalcounter = 0;
    happycounter = 0;
    while (totalcounter<10) {
    if (answer == Number(number1)+Number(number2)) {
    koala.gotoAndPlay("happy");
    totalcounter = ++totalcounter;
    happycounter=++happycounter;
    } else {
    koala.gotoAndPlay("wrong");
    totalcounter = ++totalcounter;
    }
    }
    if (totalcounter>=10) {
    gotoAndPlay("finish");
    }
    }

  2. #2
    Senior Member
    Join Date
    Apr 2000
    Location
    Northern Ireland
    Posts
    2,146
    Code:
    on (release) { 
     totalcounter = 0; 
     happycounter = 0; 
     if (totalcounter < 10) { 
      if (answer == Number(number1) + Number(number2)) { 
       koala.gotoAndPlay("happy"); 
       totalcounter ++; 
       happycounter ++; 
      } else { 
       koala.gotoAndPlay("wrong"); 
       totalcounter ++; 
      } 
     } else { 
      gotoAndPlay("finish"); 
     } 
    }
    The reason it kept going after the first click is because you had a while() loop in the code ... that meant it was executing the loop until totalcounter was no longer less than 10, which meant that your final if() statement was kicking in on the first go. Also, your increment operator statements (++) were all wrong ...

    The code above should work a little better, but there is a problem. If you set totalcounter and happycounter to 0 every time the button is pressed, they will never get above a value of 1 each, meaning that the whole counting thing will not happen. Try this code instead:

    Code:
    on (release) { 
     if (_root.countInitiated != true) {
      totalcounter = 0; 
      happycounter = 0;
      _root.countInitiated = true; 
     }
     if (totalcounter < 10) { 
      if (answer == Number(number1) + Number(number2)) { 
       koala.gotoAndPlay("happy"); 
       totalcounter ++; 
       happycounter ++; 
      } else { 
       koala.gotoAndPlay("wrong"); 
       totalcounter ++; 
      } 
     } else { 
      gotoAndPlay("finish"); 
     } 
    }
    Then in the "finish" frame, if you intend the user to have another go at the test, insert this code on the PLAY AGAIN button:
    Code:
    _root.countInitiated = false;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center