1

I am writing a quiz application. All questions and answer options are taken from xml ans shown on the screen with this code of action script

  for (i=0; i < numberOfQuestions; i++) {

                    var questionTextField = new TextField();
                    addChild(questionTextField);
                    questionTextField.text=i + "  " +  myXML.QNODE[i].QUESTION.text();
                    questionTextField.name="q"+i;

                    questionTextField.width=400;
                    questionTextField.x= 0;
                    questionTextField.y=i * 100;

                    generateAnswers(i);

                }

But, I would like to show first 5 questions and then next 5 and so on. How can we do this?

4

1 回答 1

0

You could parse the XML up-front so that you have all the questions in an Array. You could then abstract the loop which creates the questions into a function which takes parameters for the question index (which you would increment as each set of questions is completed) and the pagination limit (the number of questions to display each time). You'll also need a routine to clear up the question fields each time (you might consider storing the fields on an Array for management). Something like the following:

var questions:Array = parseXML();
showQuestions(0, 5);

    function parseXML():Array
    {
        var arr:Array = [];

        for (i=0; i < numberOfQuestions; i++) {
            arr.push(myXML.QNODE[i].QUESTION.text());
        }

        return arr;
    }

    function showQuestions(index:int, limit:int):void
    {
        for (var i:int = index; i <= (index + limit); i ++) 
        {
            var questionTextField = new TextField();
            addChild(questionTextField);
            questionTextField.text=i + "  " +  questions[i];
            questionTextField.name="q"+i;

            questionTextField.width=400;
            questionTextField.x= 0;
            questionTextField.y=i * 100;

            generateAnswers(i);     
        }
    }

    function clearQuestions():void 
    {
        // Routine to clear old questions
    }
于 2013-01-05T01:19:38.613 回答