AS3 : Random XML ?? I can only Trace! Help please...

Discussion in 'Programming' started by fdoze, May 20, 2010.

  1. #1
    Hi,

    I'm trying to set up a Quizz using Flash Action Script 3.

    For now I only have one Keyframe with Actions and one external XML file.

    I have this XML for Questions

    
    <questions>
    	<question> Question 1
    		<answer>Answer A 1</answer>
    		<answer> Answer B 1</answer>
    		<answer> Answer C 1</answer>
    	</question>
    	< question> Question 2
    		<answer> Answer A 2 </answer>
    		<answer> Answer B 2</answer>
    		<answer> Answer C 2</answe >
    	</question>
    	<question> Question 3
    		<answer> Answer A 3 </answer>
    		<answer> Answer B 3</answer>
    		<answer> Answer C 3</answer>
    	</question>
    	<question>Question 4
    		<resposta1> Answer A 4</resposta1>
    		<resposta2> Answer B 4</resposta2>
    		<resposta3> Answer C 4</resposta3>
    	</question>
    </questions>
    
    Code (markup):
    And this is my AS3

    
    
    function radomizeXML(source:XML, nodes:int = 4):XMLList
    {
    	if (source.*.length() < nodes) return source.*;
    	var list:XMLList;
    	var hash:Array = [];
    	var randomPosition:int;
    	var node:XML;
    	do
    	{
    		node = source.*[(Math.random() * source.*.length()) >> 0];
    		if (hash.indexOf(node.toXMLString()) < 0)
    		{
    			hash.push(node.toXMLString());
    			list ? list += node : list = XMLList(node);
    		}
    	} while (list.length() < nodes)
    	return list;
    }
    
    var xmlLoader:URLLoader = new URLLoader();
    xmlLoader.addEventListener(Event.COMPLETE, showXML);
    xmlLoader.load(new URLRequest("perguntas.xml"));
    
    function showXML(e:Event):void {
    	XML.ignoreWhitespace = true; 
    	
    	var myQuestions = new XML(e.target.data);
    	
    	trace(radomizeXML(myQuestions).toXMLString()); // THIS WORKS!!!
    	
    	var myQuestionsRandom:XML = radomizeXML(myQuestions).toXMLString(); // THIS GETS ERROR!
    	
    	
    	trace(myQuestionsRandom. question.length());
    	
    	var i:Number;
    		for (i=0; i < myQuestionsRandom.question.length(); i++) { // ERROR HERE TOO! IF :XML not set.
    			trace(" Q: "+ myQuestionsRandom. question[i].text());
    			trace(" A1: "+ myQuestionsRandom. question[i].answer.text());
    			trace(" A2: "+ myQuestionsRandom. question[i]. answer.text());
    			trace(" A3: "+ myQuestionsRandom.question[i]. answer.text());
    			trace(" ----- ");
    		}
    }
    
    Code (markup):
    My problem is that I can trace my Random XML Result.

    But When I try to trace and loop results I get error!



    Could you please help me on this?

    Thanks.
     
    fdoze, May 20, 2010 IP
  2. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #2
    Why not parse the XML results to strongly typed objects, put it in a custom collection, then put a method in that collection obj to pull a random item...
     
    ccoonen, May 20, 2010 IP
  3. fdoze

    fdoze Peon

    Messages:
    205
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Sorry, I did not understand.


    Could you please explain with some example code?

    Thanks!
     
    fdoze, May 20, 2010 IP
  4. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #4
    How I would build it is with an IAnswer, Answer, and Resposta1 as Value Objects and one interface. Answer and Resposta1 implement IAnswer and all that is in IAnswer is a getter called answer as a string.

    Next create VO's for your Question Objects, then create a QuestionCollection object that holds the questions and has helper methods like getByID(id:int):QuestionVO

    So, iterate top down next, iterate your xml nodes over every Question, build a Question Object with an ID (which should live in the XML... this will tie the answers to the Question).

    Then, iterate over your answer xml and push it through a factory which object to create, an Answer or a Respota1 object. Then push that into the Question object array.

    Arg... I could just do it for you for some cash, hehe... and you'd have a hella slick flash app :)
     
    ccoonen, May 24, 2010 IP