2 Replies Latest reply on Jan 19, 2013 4:01 PM by razzledee

    Actionscript 2 - set score to reduce with timer

    razzledee

      I purchased an xml/actionscript 2.0 quiz script and for the most part it does what I want but I would like to tweek it so that it's more challenging to players. I know little about actionscript hence why I purchased the script and sadly the writer isn't being any help.

       

      I would like the score for each question to reduce over time. For instance the player has 30 secs to answer... each question is worth 30 points but reduces by 1 point for every sec it takes to answer. I don't know where to begin to do this.

       

      The code allows for default time and score for all questions or can be set to have seperate time and score for an individual question.

       

      This is the xml code that is loaded by the actionscript (I've included just 1 question) ...

       

      <?xml version="1.0" encoding="UTF-8"?>

      <quiz DefaultTimeInSeconds="30" DefaultPoints="30" NumberOfQuestions="10" RandomQuestions="TRUE" DefaultIfCorrectAnswer="Answer is correct!" DefaultIfWrongAnswer="Wrong Answer! Correct answer is " TimeIsUp="Time is Up !">

        <node>

      <question><![CDATA[<b>What is the capital of Serbia?</b>]]></question>

      <answers correctAnswer="Belgrade">

        <option>Tobolsk</option>

        <option>Belgrade</option>

        <option>Sofia</option>

        <option>Bucharest</option>

      </answers>

      <options image="images/beograd.jpg" explanationIfCorrect="That is correct! You won 7 points." explanationIfNotCorrect="Sorry, you are wrong. The correct answer is Belgrade."  timeInSeconds="10" points="7"></options>

        </node>

      </quiz>

       

      and the actionscript...

       

        stop();

        // buttons mc are disabled and hidden at load
        Answer0Button.enabled=false; button0._visible=false;
        Answer1Button.enabled=false; button1._visible=false;
        Answer2Button.enabled=false; button2._visible=false;
        Answer3Button.enabled=false; button3._visible=false;
       
        function countdown() {
         counter--;
         countdown_txt.text = counter;
         if (counter == 0) {
          Answer0Button.enabled=false;
          Answer1Button.enabled=false;
          Answer2Button.enabled=false;
          Answer3Button.enabled=false;
          clearInterval(intID);
          AnswerPopUp_mc.gotoAndPlay(1);
          AnswerPopUp_mc.AnswerPopUp.DisplayResult.htmlText = "" + TimeIsUp + "";
         }
        }

        // defining variables for data
        var ChosenNumberOfQuestions = int(0); // Number of questions in quiz
        var TotalNumberOfQuestions = int(0); // Total number of questions in XML file
        var NumberOfQuestions = int(0); // min of two above
        var QuestionCounter = int(0); // for dynamicaly showing questions
        var CurrentQuestion = QuestionCounter + 1; // for dynamicaly showing questions
        var Points = 0; // for each question
        var TotalPoints = 0; // Max Points
        var Score = 0; // display total score
        var NumberOfCorrectAnswers = 0; // counter for questions answered correctly

        var RandOrderQuestions = new Array();
        var Questions = new Array();
        var Images = new Array();
        var CorrectAnswers = new Array();
        var Answers = new Array();
        var ExplanationsIfCorrect = new Array();
        var ExplanationsIfNotCorrect = new Array();
        var TimesForSolving = new Array();
        var NumberOfPoints = new Array();

        
        // roll over states for buttons
        button0.onEnterFrame = function() {
         if (mouse_over_button0) {
          _root.button0.nextFrame();
         } else {
          _root.button0.prevFrame();
         }
        };
        button1.onEnterFrame = function() {
         if (mouse_over_button1) {
          _root.button1.nextFrame();
         } else {
          _root.button1.prevFrame();
         }
        };
        button2.onEnterFrame = function() {
         if (mouse_over_button2) {
          _root.button2.nextFrame();
         } else {
          _root.button2.prevFrame();
         }
        };
        button3.onEnterFrame = function() {
         if (mouse_over_button3) {
          _root.button3.nextFrame();
         } else {
          _root.button3.prevFrame();
         }
        };
       
        // loading data from XML
        var xmlData = new XML();
        xmlData.ignoreWhite = true;

        xmlData.onLoad = function () {
        
         // Default Time for solving
         DefaultTimeInSeconds = this.firstChild.attributes.DefaultTimeInSeconds;
          trace ("DefaultTime: " + DefaultTimeInSeconds);
         // Default Points
         DefaultPoints = this.firstChild.attributes.DefaultPoints;
          trace ("DefaultPoints: " + DefaultPoints);
         // Default comment on answers
         DefaultIfCorrectAnswer = this.firstChild.attributes.DefaultIfCorrectAnswer;
         DefaultIfWrongAnswer = this.firstChild.attributes.DefaultIfWrongAnswer;
        
         // Default if time is up
         TimeIsUp = this.firstChild.attributes.TimeIsUp;

         // Random order of questions or not
         RandomQuestions = this.firstChild.attributes.RandomQuestions;
          trace ("RandomQuestions: " + RandomQuestions);
         
         // Number Of Questions
         ChosenNumberOfQuestions = this.firstChild.attributes.NumberOfQuestions;
          trace ("ChosenNumberOfQuestions: " + ChosenNumberOfQuestions);
         
         // Counting  total number of Questions in XML file
         var nodes = this.firstChild.childNodes;
         for (var a=0; a<nodes.length; a++){
          TotalNumberOfQuestions = a+1;
         } 
         trace ("TotalNumberOfQuestions: " + TotalNumberOfQuestions);

         // Finaly - number of questions in QUIZ - min of two above
         if (ChosenNumberOfQuestions < TotalNumberOfQuestions) {
          NumberOfQuestions = ChosenNumberOfQuestions;
         } else {
          NumberOfQuestions = TotalNumberOfQuestions;
         }
        
         // Random order Questions or not
         // first we populate array with question numbers
         for(i=0; i<TotalNumberOfQuestions; i++){
          RandOrderQuestions[i] = i;
          trace(RandOrderQuestions);
         }
         // if we want random questions we use random sorting of number of questions
         if (RandomQuestions == "TRUE") {
          RandOrderQuestions.sort(function () {
           return random(2) ? true : false;
          });
          trace(RandOrderQuestions);
         }
         ///////////////////////////
         // POPULATING FROM XML
         for (var i=0; i < NumberOfQuestions; i++ )
         {
          // Chose the number of question from RandOrderQuestions array
          var numInXML = RandOrderQuestions[i];
          trace("Question number in XML: " + numInXML);
         
          // populating questions from XML
          Questions[i] = this.firstChild.childNodes[numInXML].childNodes[0].firstChild.nodeValue;
          //trace(Questions);

          //populating Correct Answers from XML
          CorrectAnswers[i] = this.firstChild.childNodes[numInXML].childNodes[1].attributes.correctAnswer;
          //trace(CorrectAnswers);
         
          // populating options for answers from XML
          var NewArray = new Array(); // temp array for answers for each question
          for (var j=0; j < this.firstChild.childNodes[numInXML].childNodes[1].childNodes.length; j++ )
           {
           trace(i + " "+ j);
           NewArray[j] = this.firstChild.childNodes[numInXML].childNodes[1].childNodes[j].firstChild.nodeValue;
           //trace(NewArray);
           NewArray.sort(function () {
           return random(2) ? true : false;
           });

           }
          Answers.push(NewArray); // push answers for this i question in main Answers array
        
          //populating additional options from XML
          ExplanationsIfCorrect[i] = this.firstChild.childNodes[numInXML].childNodes[2].attributes.explanationIfCorrect;
          ExplanationsIfNotCorrect[i] = this.firstChild.childNodes[numInXML].childNodes[2].attributes.explanationIfNotCorrect;
          TimesForSolving[i] = this.firstChild.childNodes[numInXML].childNodes[2].attributes.timeInSeconds;
          NumberOfPoints[i] = this.firstChild.childNodes[numInXML].childNodes[2].attributes.points;
          Images[i] = this.firstChild.childNodes[numInXML].childNodes[2].attributes.imageIfCorrect;
          Images[i] = this.firstChild.childNodes[numInXML].childNodes[2].attributes.imageIfIncorrect;
         
         }
         trace("Questions: " + Questions); trace("Correct Answers: " + CorrectAnswers);
         trace(NewArray);
         trace(ExplanationsIfCorrect); trace(ExplanationsIfNotCorrect); trace(TimesForSolving); trace(NumberOfPoints);  trace(Images);
         
         // show first question (QuestionCounter=0)
         QuestionNumber.text = "Question #" + 1;
         QuestionDisplay.htmlText = Questions[QuestionCounter];
         // show answers for first question (QuestionCounter=0)
         if (0 < Answers[QuestionCounter].length){ Answer0Display.text = Answers[QuestionCounter][0]; Answer0Button.enabled=true; button0._visible=true;} else { button0._visible=false;}
         if (1 < Answers[QuestionCounter].length){ Answer1Display.text = Answers[QuestionCounter][1]; Answer1Button.enabled=true; button1._visible=true;} else { button1._visible=false;}
         if (2 < Answers[QuestionCounter].length){ Answer2Display.text = Answers[QuestionCounter][2]; Answer2Button.enabled=true; button2._visible=true;} else { button2._visible=false;}
         if (3 < Answers[QuestionCounter].length){ Answer3Display.text = Answers[QuestionCounter][3]; Answer3Button.enabled=true; button3._visible=true;} else { button3._visible=false;}
         // show image if defined
         if (Images[QuestionCounter]){ loadMovie(Images[QuestionCounter],"imgContainer"); } else {  }
         // determine the number of points
         if (NumberOfPoints[QuestionCounter]){ Points = NumberOfPoints[QuestionCounter] } else { Points = DefaultPoints }
        
         // start counter
         if (TimesForSolving[QuestionCounter]){
          countdown_time = TimesForSolving[QuestionCounter]
         } else {
          countdown_time = DefaultTimeInSeconds
         } // now call the function
          counter = countdown_time;
          countdown_txt.text = countdown_time;
          clearInterval(intID);
          intID = setInterval(countdown,1000);
         }

        xmlData.load("questions.xml");

       

      Thanks much in advance.

       

      Cheers