Question
HOW TO UPDATE QuizGame.as & QuestionBank.as CLASSES TO ACCEPT JSON PARSED DATA? (ActionScript) My app isn't displaying the data for some reason. JSON DATA STRUCTURE
HOW TO UPDATE QuizGame.as & QuestionBank.as CLASSES TO ACCEPT JSON PARSED DATA? (ActionScript) My app isn't displaying the data for some reason.
JSON DATA STRUCTURE
{
"questions": [
{
"q":"For Socrates, an unexamined life is a tragedy because it results in grievous harm to _____.",
"answers":[
{
"A":"A.the soul",
"B":"B.the body",
"C":"C.the state"
}
],
"correct":"A"
},
QuestionBank.as CLASS
package com.robertosborn {
import flash.events.EventDispatcher;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.Event;
public class QuestionBank extends EventDispatcher {
private const JSON_URL:String = "data/questions.json";
private var jsonRequest:URLRequest;
private var jsonLoader:URLLoader;
private var questionArray:Array;
public function QuestionBank() {
questionArray = new Array();
loadJSON();
}
private function loadJSON():void {
jsonRequest = new URLRequest(JSON_URL);
jsonLoader = new URLLoader;
jsonLoader.addEventListener(Event.COMPLETE, jsonLoaded);
jsonLoader.load(jsonRequest);
}
private function jsonLoaded(e:Event):void {
var jsonData:Object = JSON.parse(e.target.data);
for each (var question:Object in jsonData.questions){
questionArray.push(questions);
}
dispatchEvent(new Event(Event.COMPLETE));
}
public function buildBank():Array {
var b:Array = new Array();
var max:int = questionArray.length;
for(var i:int=0; i
b.push({q:questions.questionArray[i].q, answers:questions.questionArray[i].answers, correct:questions.questionArray[i].correct}); //CORRECT?
}
return b;
}
}
}
QuizGame.as CLASS
package com.robertosborn { import flash.display.MovieClip; import flash.text.TextField; import flash.display.SimpleButton; import flash.events.Event; import com.robertosborn.QuestionBank; import flash.events.MouseEvent; import flash.utils.setTimeout; public class QuizGame extends MovieClip { public var aBtn:SimpleButton; public var bBtn:SimpleButton; public var cBtn:SimpleButton; public var questionText:TextField; public var statusMsg:TextField; private var questionBank:QuestionBank; private var questionArray:Array; private const questionMax:int = 5; private var questionCount:int = 0; private var currentAnswer:String; private var currentScore:int = 0; public function QuizGame() { statusMsg.text = ""; questionArray = new Array(); questionBank = new QuestionBank(); questionBank.addEventListener(Event.COMPLETE, dataReady); aBtn.addEventListener(MouseEvent.CLICK, aPressed); bBtn.addEventListener(MouseEvent.CLICK, bPressed); cBtn.addEventListener(MouseEvent.CLICK, cPressed); } private function dataReady(e:Event):void { questionArray = questionBank.buildBank(); grabQuestion(); } private function grabQuestion():void { if(questionCount < questionMax) { var max:int = questionArray.length-1; var rq:int = Math.floor(Math.random() * (max - 0 + 1)); var ta:Array = questionArray.splice(rq, 1); questionText.text = ta[0].q; currentAnswer = ta[0].correct; questionCount++; } else { gameOver(); } } private function gameOver():void { statusMsg.text = currentScore+"/100 points"; questionText.text = "Game Over!"; } private function aPressed(e:MouseEvent):void { //HERE? if(statusMsg.text == ""){ if(currentAnswer == "A"){ currentScore += 100/questionMax; statusMsg.text = "CORRECT!"; }else{ statusMsg.text = "WRONG"; } setTimeout(newQuestion, 1000); } } private function bPressed(e:MouseEvent):void { //HERE? if(statusMsg.text == ""){ if(currentAnswer == "B"){ currentScore += 100/questionMax; statusMsg.text = "CORRECT!"; }else{ statusMsg.text = "WRONG"; } setTimeout(newQuestion, 1000); } }
private function cPressed(e:MouseEvent):void { //HERE? if(statusMsg.text == ""){ if(currentAnswer == "C"){ currentScore += 100/questionMax; statusMsg.text = "CORRECT!"; }else{ statusMsg.text = "WRONG"; } setTimeout(newQuestion, 1000); } } private function newQuestion():void { statusMsg.text = ""; grabQuestion(); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started