Flash Builder,进行测验,随机化问题和点

时间:2013-07-01 16:24:43

标签: xml flash-builder

我在Flash Builder上创建了一个测验。我总共有40个问题,我希望我的测验可以随机化,只显示10个问题。

我的每个问题都有3个选择答案,其中包含10个标记,5个标记和1个标记。所以,无论如何我可以添加一些编码,以便让它在10个问题后积累得分?

1 个答案:

答案 0 :(得分:0)

请查看此示例,可能会有用:

<?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>

}

这是 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>

如果您想测试此示例,请访问 LINK source )。

<强>被修改

您必须考虑以下事项: 的(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>

对于每个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>

最后(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>