2 Replies Latest reply on Apr 17, 2010 4:29 AM by armandix

    Help me with this quiz

    armandix Level 1

      I am developing a quiz game, He will get the questions to an xml, I would create a random, but this have  to select different answers without repeat.

      I need make a code  to tell me how many nodes have xml, and after I select a node at random and return me, but then this never happens again.

      someone help me?

        • 1. Re: Help me with this quiz
          Harry Kunz Level 4

          You can get the number of nodes nNodes like this:

           

          var nNodes:Number;

          var cXmlData:XML = new XML();

          cXmlData.ignoreWhite = true;

          cXmlData.onLoad = function() {

              if (fLoadSuccess) {

                  if (cXmlData.status == 0) {

                      var cNode:XMLNode = cXmlData.firstChild;

                      nNodes = cNode.childNodes.length;

                  }

              }

          };

          cXmlData.load("your xml file path");

           

          Then you can get the question from the xml after load completion:

           

          var nQuestion:Number = Math.random()*nNodes

          var sQuestion:String = cXmlData.firstChild.childNodes[nQuestion].firstChild.nodeValue;

           

          Then store the value nQuestion and use it for future checks that it already was used.

          1 person found this helpful
          • 2. Re: Help me with this quiz
            armandix Level 1

            Harry tanks for your help,

            but still unable to resolve, soo in my first frame i have this code

             

            function onQuizData(success)

            {

                var quizNode = this.firstChild;

                var quizTitleNode = quizNode.firstChild;

                title = quizTitleNode.firstChild.nodeValue;

                var i = 0;// o numero complexo i=0

                var itemsNode = quizNode.childNodes[1];

                while (itemsNode.childNodes[i])

                {

                    var itemNode = itemsNode.childNodes[i];

                    var questionNode = itemNode.childNodes[0];

                    quizItems[i] = new QuizItem(questionNode.firstChild.nodeValue);

                    var a = 1;

                    var answerNode = itemNode.childNodes[a++];

                    while (answerNode)

                    {

             

                        var isCorrectAnswer = false;

                        if (answerNode.attributes.correct == "y")

                        {

                            isCorrectAnswer = true;

                        } // end if

                        quizItems[i].addAnswer(answerNode.firstChild.nodeValue, isCorrectAnswer);

                        answerNode = itemNode.childNodes[a++];

                    } // end while

                    ++i;

                } // end while

                gotoAndPlay(_currentframe + 1);

            } // End of the function

            var quizItems = new Array();

            var myData = new XML();

            myData.ignoreWhite = true;

            myData.onLoad = onQuizData;

            myData.load("questionario/questionario.xml");

            stop ();

             

            -- 2º frame--

            this frame contain the variable to the frame where I have the quiz

            var valores = quizItems.length;
            var currentQuestionNumber = 1;
            var numOfQuestionsAnsweredCorrectly = 0;
            var numOfQuestionsAnsweredIncorrectly = 0;
            var d1 = 1;
            trace(valores);
            --3ºframe--
            this is the frame where i wont select the aleatory response without repeat
            if (numeracao < quizItems.length)
            {
            var array:Array = new Array(quizItems.length);
            currentQuestionNumber = Math.round(Math.random() * valores);
            }
            else if (numeracao > quizItems.length){
            gotoAndStop(22);
            }
            trace(currentQuestionNumber);
            var currentQuizItem = quizItems[currentQuestionNumber - 1];
            var hasAnswered = false;
            question = currentQuizItem.getQuestion();
            var i = 1;
            while (i <= currentQuizItem.getNumOfAnswers())
            {
                _root["answer" + i] = currentQuizItem.getAnswer(i - 1);
                _root["answer" + i + "textBox"]._visible = true;
                _root["answer" + i + "Button"]._visible = true;
                _root["answer" + i + "Mark"]._visible = true;
                ++i;
            } // end while
            while (i <= 4)
            {
                _root["answer" + i + "textBox"]._visible = false;
                _root["answer" + i + "Button"]._visible = false;
                _root["answer" + i + "Mark"]._visible = false;
                ++i;
            } // end while
            stop ();