我在 Flash Builder 上创建了一个测验。我总共有 40 个问题,我希望我的测验会随机化并且只显示 10 个问题。
我的每个问题都有 3 个多项选择答案,其中包含 10 分、5 分和 1 分。那么无论如何我可以添加一些编码以让它在10个问题后累积分数吗?
我在 Flash Builder 上创建了一个测验。我总共有 40 个问题,我希望我的测验会随机化并且只显示 10 个问题。
我的每个问题都有 3 个多项选择答案,其中包含 10 分、5 分和 1 分。那么无论如何我可以添加一些编码以让它在10个问题后累积分数吗?
请检查这个例子,也许会有用:
<?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>
已编辑
您必须考虑以下事项: (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>
对于每个视图 (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>