Search code examples
xmlflash-builder

Flash Builder, doing a quiz , randomize questions and points


I have created a quiz on Flash Builder. I have a total of 40 questions, which i hope my quiz will randomize and show only 10 questions.

Each of my question has 3 multiple choice answers which contains 10 marks, 5 marks and 1mark. So is there anyway i can add some coding in order to let it accumulate the score after 10 questions?


Solution

  • Please check this example, maybe will be useful:

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()" xmlns:local="*">
        <mx:Script>
            <![CDATA[
                import mx.collections.ArrayCollection;
                import mx.controls.Alert;
                import mx.containers.Panel;
            private var arrAnswers:Array = ["Red","Yellow","Blue","Red","Orange"];  //this array contains the answers
            private var arrComp:Array;
            private var lastChild:Number = 0;
    
            public function init():void{
                arrComp = [q1,q2,q3,q4,q5];
                main.selectedChild = arrComp[0];
            }
    
            public function processQ(i:Number):void{
                lastChild = lastChild + i;
                if(lastChild==4){
                    next.enabled = false;
                    prev.enabled = true;
                }else if(lastChild==0){
                    prev.enabled = false;
                    next.enabled = true;
                }else{
                    next.enabled = true;
                    prev.enabled = true;
                }
                main.selectedChild = arrComp[lastChild];
                updateSelected();
            }
    
            private function updateSelected():void{
                var tmpArr:ArrayCollection = new ArrayCollection();
    
                tmpArr.addItem("1:" + String(q1.radioGroup.selectedValue));
                tmpArr.addItem("2:" + String(q2.radioGroup.selectedValue));
                tmpArr.addItem("3:" + String(q3.radioGroup.selectedValue));
                tmpArr.addItem("4:" + String(q4.radioGroup.selectedValue));
                tmpArr.addItem("5:" + String(q5.radioGroup.selectedValue));
                lstSelected.dataProvider = tmpArr;
            }
    
            public function calculateTotal():void{
                var i:int = 0;
                var nCorrect:int = 0;
                for each(var obj:QuestionComponent in arrComp){
                    if(obj.radioGroup.selectedValue==arrAnswers[i]){
                        nCorrect++;
                    }
                    i++;
                }
                lblC.text = "Correct answers: " + nCorrect.toString();
            }
        ]]>
    </mx:Script>
    <mx:ViewStack id="main" width="100%">
        <local:QuestionComponent title="Question N°1" id="q1" />
        <local:QuestionComponent title="Question N°2" id="q2" />    
        <local:QuestionComponent title="Question N°3" id="q3" />    
        <local:QuestionComponent title="Question N°4" id="q4" />    
        <local:QuestionComponent title="Question N°5" id="q5" />        
    </mx:ViewStack>
    <mx:HBox width="100%">
        <mx:Grid width="100%" verticalAlign="bottom">
            <mx:GridRow horizontalAlign="left" width="100%" verticalAlign="bottom">
                <mx:GridItem horizontalAlign="left" width="33%">
                    <mx:Button id="prev" click="processQ(-1)" label="Prev" enabled="false" width="100%"/>
                </mx:GridItem>
                <mx:GridItem horizontalAlign="center" width="33%">
                    <mx:Button id="tot" click="calculateTotal()" label="GetTotal" width="100%"/>
                </mx:GridItem>              
                <mx:GridItem horizontalAlign="right" width="33%">
                    <mx:Button id="next" click="processQ(1)" label="Next" width="100%"/>    
                </mx:GridItem>
            </mx:GridRow>
        </mx:Grid>
    </mx:HBox>
    <mx:List id="lstSelected" width="200" height="200"/>
    <mx:HBox horizontalAlign="left">
        <mx:Text text="Correct answers: 0" id="lblC" fontWeight="bold" fontSize="50"/>  
    </mx:HBox>
    

    }

    And this is a component QuestionComponent:

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%">
        <mx:RadioButtonGroup id="radioGroup"/>
        <mx:VBox>
            <mx:RadioButton id="radioButton1" label="Red" group="{radioGroup}" />
            <mx:RadioButton id="radioButton2" label="Orange" group="{radioGroup}" />
            <mx:RadioButton id="radioButton3" label="Yellow" group="{radioGroup}" />
            <mx:RadioButton id="radioButton4" label="Green" group="{radioGroup}"/>
            <mx:RadioButton id="radioButton5" label="Blue" group="{radioGroup}" />      
        </mx:VBox>
    </mx:Panel>
    

    If you want to test this example please visit this LINK (source).

    Edited

    You must consider the following: (quizz.mxml)

    <s:ViewNavigatorApplication ...>
    ......
    <fx:Script>
        <![CDATA[
            public var answersArr:Array = null; 
    
            public function init():void{
                answersArr = [0,0,0,0,0,0,0,0,0,0];
            }
        ]]>
    </fx:Script>
    ......
    </s:ViewNavigatorApplication>
    

    For each VIEW (qns1,qns2,....qns10):

    <s:View ....>
        <fx:Script>
            <![CDATA[
                public var selected:int = 0;
                public function next():void{
                    parentDocument["answersArr"][0] = selected;
                    navigator.pushView(qns2);
                }
            ]]>
        </fx:Script>
        <s:Button x="230" y="345" label="Next" click="next()"/>
    ....
        <s:Button x="33" y="135" width="261" label="A." click="{selected=1;}"/>
        <s:Button x="33" y="208" width="261" label="B." click="{selected=2;}"/>
        <s:Button x="31" y="283" width="261" label="C." click="{selected=3;}"/>
    ....
    </s:View>
    

    And finally (summary.mxml):

    <s:View ...>
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            public var selected:int = 0;
            public function view_activateHandler():void{
                var arr:Array = parentDocument["answersArr"] as Array;
                var tempArr:ArrayCollection = new ArrayCollection();
                var j:int = 1;
                for each(var i:int in arr){
                    tempArr.addItem("Question N°" + j.toString() + " - Answer: " + i.toString());
                    j++;
                }
                answers.dataProvider = tempArr;
            }
        ]]>
    </fx:Script>
    ....
        <s:List x="27" y="224" width="266" height="140" id="answers" />
    </s:View>