SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Jul 2002
    Location
    Italy
    Posts
    429
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    AS loop - can't figure out why

    I have a small Flash app in Flash 8 - a questionnaire which has any number of questions with 4 multi-choice answers. I have just changed it so there may be up to 4 multi-choice answers. It seems to work fine with each question but when it gets to the last question it seems to go into an interminable loop. The termination code hasn't changed and I have tried trace() on just about every variable and cannot for the life of me see what is wrong. Can anyone help? Please - ever so nice please...

    Before:
    Code:
    if (currentQuestionNumber>quizItems.length) gotoAndStop("Summary"); 
    
     var currentQuizItem=quizItems[currentQuestionNumber-1];
     var hasAnswered=false;
     question=currentQuizItem.getQuestion();
     for (var i=1; i<=4; i++) {
         _root["answer"+i]=currentQuizItem.getAnswer(i-1);
     }
     stop();
    After:
    Code:
    if (currentQuestionNumber>quizItems.length) gotoAndStop("Summary"); 
    
     var currentQuizItem=quizItems[currentQuestionNumber-1];
     var hasAnswered=false;
     question=currentQuizItem.getQuestion();
     for (var i=1; i<=currentQuizItem.getNumOfAnswers(); i++) {
         _root["answer"+i]=currentQuizItem.getAnswer(i-1);
         _root["answer"+i+"Mark"]._visible=true;
         _root["answer"+i+"Textbox"]._visible=true;
         _root["answer"+i+"Button"]._visible=true;
     }
     for (; i<=4; i++) {
         _root["answer"+i+"Mark"]._visible=false;
         _root["answer"+i+"Textbox"]._visible=false;
         _root["answer"+i+"Button"]._visible=false;
     }
     stop();
    and
    Code:
    this.getNumOfAnswers=function() {
    return this.answers.length;
    }
    I have uploaded my FLA file to http://www.compassion-in-business.co...s/quiz_mbb.fla

    PS I've found that if I change
    for (var i=1; i<=currentQuizItem.getNumOfAnswers(); i++)
    to
    for (var i=1; i<=4; i++)
    the script doesn't loop - but of course it doesn't solve my problem, just makes it seem stranger!

    PPS I have some AS on some of the buttons and wonder if mixing AS1 and AS2 is (part of) the problem although that still doesn't seem to explain.
    Last edited by gandalf458; Jul 26, 2008 at 09:01. Reason: further thoughts
    blessed are the cracked for they let in the light

  2. #2
    JavaScript Guru (Big Ego) Arielladog's Avatar
    Join Date
    Jul 1999
    Location
    SC, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm mostly a Flex developer, and I don't have much Flash/AS 2 experience, but I decided to take a look at the file since the logic looked correct to me.

    So my XML didn't download, so currentQuestionNumber was 1 and quizItems.length was 0. This meant the gotoAndStop() method was called. However, I noticed the rest of the code was still being run. I thought gotoAndStop() wasn't working, but I later figured out it's just not what I thought. It does indeed send it to the right frame and stops Flash player from continuing. However, it doesn't do it immediately--the rest of the code for the frame still runs. Given the 1, 0 values from above, this causes an infinite loop.

    So to stop this, just do something like:

    Code:
    if (currentQuestionNumber>quizItems.length) 
    {
        gotoAndStop("Summary");
    }
    else
    {
        var currentQuizItem=quizItems[currentQuestionNumber-1];
        var hasAnswered=false;
        question=currentQuizItem.getQuestion();
        for (var i=1; i<=currentQuizItem.getNumOfAnswers(); i++) {
        _root["answer"+i]=currentQuizItem.getAnswer(i-1);
        _root["answer"+i+"Mark"]._visible=true;
        _root["answer"+i+"Textbox"]._visible=true;
        _root["answer"+i+"Button"]._visible=true;
        }
        for (; i<=4; i++) {
        _root["answer"+i+"Mark"]._visible=false;
        _root["answer"+i+"Textbox"]._visible=false;
        _root["answer"+i+"Button"]._visible=false;
        }
        stop();
    }
    Hope that helps,
    Ryan

  3. #3
    SitePoint Evangelist
    Join Date
    Jul 2002
    Location
    Italy
    Posts
    429
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ryan - many thanks. That does seem strange. I'll try that as soon as I get back to my Windows machine.

    Cheers G
    blessed are the cracked for they let in the light


Bookmarks

Posting Permissions

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