Hi, I’m making a quiz. But i’m having a bit of a problem. I want the answers to go to a random position. It works for a 2 answers. But now i want to make it have 4 answers.
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
public class Main extends Sprite {
private var questionField:TextField=new TextField ;
private var answerField1:TextField=new TextField ;
private var answerField2:TextField=new TextField ;
private var answerField3:TextField=new TextField ;
private var answerField4:TextField=new TextField ;
private var resultField:TextField=new TextField ;
private var questions:Array=new Array ;
private var questionIndex:int=-1;
public function Main():void {
if (stage) {
init();
} else {
addEventListener(Event.ADDED_TO_STAGE,init);
}
}
private function init(e:Event=null):void {
removeEventListener(Event.ADDED_TO_STAGE,init);
questionField.y=150;
answerField1.y=275;
answerField1.x=-50;
answerField2.y=350;
answerField2.x=-50;
answerField3.y=275;
answerField3.x=50;
answerField4.y=350;
answerField4.x=50;
resultField.y=300;
questionField.selectable=answerField1.selectable=answerField2.selectable=answerField3.selectable=answerField4.selectable=resultField.selectable=false;
questionField.width=answerField1.width=answerField2.width=answerField3.width=answerField4.width=resultField.width=stage.stageWidth;
questionField.autoSize=answerField1.autoSize=answerField2.autoSize=answerField3.autoSize=answerField4.autoSize=resultField.autoSize=TextFieldAutoSize.CENTER;
questionField.defaultTextFormat=resultField.defaultTextFormat=new TextFormat('Arial',18,0xffffff,true);
answerField1.defaultTextFormat=answerField2.defaultTextFormat=answerField3.defaultTextFormat=answerField4.defaultTextFormat=new TextFormat('Arial',15,0xffffff);
addChild(questionField);
addChild(answerField1);
addChild(answerField2);
addChild(answerField3);
addChild(answerField4);
addChild(resultField);
answerField1.addEventListener(MouseEvent.CLICK,answerClick);
answerField2.addEventListener(MouseEvent.CLICK,answerClick);
answerField3.addEventListener(MouseEvent.CLICK,answerClick);
answerField4.addEventListener(MouseEvent.CLICK,answerClick);
questions.push('Howmuch is 1+1?',['2','1','3','4'],'Howmuch is 1+2',['3','2','1','4'],'Howmuch is 2+2?',['4','3','2','1'],'Howmuch is 2-1?',['1','2','3','4']);
resultField.addEventListener(MouseEvent.CLICK,nextQuestion);
nextQuestion();
}
private function answerClick(e:MouseEvent):void {
var answer:String=e.currentTarget==answerField1 ? answerField1.text : answerField2.text;
questionField.visible=answerField1.visible=answerField2.visible=answerField3.visible=answerField4.visible=false;
resultField.visible=true;
if (questions[questionIndex*2+1][0]==answer) {
resultField.text='Correct.';
} else {
resultField.text='wrong try again.';
questionIndex=-1;
}
}
private function nextQuestion(e:MouseEvent=null):void {
questionIndex++;
if (questionIndex*2>=questions.length) {
answerField2.removeEventListener(MouseEvent.CLICK,answerClick);
answerField2.addEventListener(MouseEvent.CLICK,restart);
questionField.text='win.';
answerField2.text='Again?';
resultField.visible=false;
questionField.visible=answerField2.visible=true;
return;
}
resultField.visible=false;
questionField.visible=answerField1.visible=answerField2.visible=answerField3.visible=answerField4.visible=true;
questionField.text=questions[questionIndex*2];
var qo:uint=Math.floor(Math.random()*2);
answerField1.text=questions[questionIndex*2+1][qo];
answerField2.text=questions[questionIndex*2+1][Math.abs(qo-1)];
answerField3.text=questions[questionIndex*2+1][Math.abs(qo-2)];
answerField4.text=questions[questionIndex*2+1][Math.abs(qo-3)];
}
private function restart(e:MouseEvent=null):void {
questionIndex=-1;
nextQuestion();
answerField2.removeEventListener(MouseEvent.CLICK,restart);
answerField2.addEventListener(MouseEvent.CLICK,answerClick);
}
}
}
This is my code.
Can anybody help me?