samedi 21 janvier 2017

AS3: How would I make the answers pop up green or red if the answer was right or not in a quiz?

I got this code from my tutor and the quiz itself works; you can select answers and it traces that you've done so, but it doesn't recognise if you've pressed the right answer or not.

I want it to highlight green or red depending on if the right answer was clicked; but i don't know what code to right as i'm still a beginner at Action script. The correct answer is specified in the XML I just have to reference it to the code i think. Any help?

Here is the code:

package {

import flash.display.MovieClip;

import flash.text.TextField;

import flash.text.TextFormat;

import flash.text.TextFieldAutoSize;

import flash.net.URLLoader;

import flash.net.URLRequest;

import flash.events.Event;

import flash.events.MouseEvent;

import TabAnswer;

import IconNext;


public class Quiz extends MovieClip {

    private var tf:TextFormat = new TextFormat();

    private var question:TextField = new TextField();

    private var answer:TabAnswer;

    private var answers:Array = new Array();

    var answerArray:Array = new Array ("9","Pitcher");

    private var quiz:XML;       

    private var next:IconNext = new IconNext();

    private var currentQuestion:int = 0;


    public function Quiz() {
        // constructor code

        var xmlLoader:URLLoader = new URLLoader();

        xmlLoader.addEventListener( Event.COMPLETE, xmlLoaded );

        xmlLoader.load( new URLRequest( "quiz.xml" ) );

    }

    private function xmlLoaded ( e:Event ):void
    {

        // make an XML instance using XML data passed to this function
        quiz = new XML( e.target.data );

        trace( quiz );

        //set some text formatting

        tf.size = 20;

        tf.color = 0x000000;

        tf.font = "Tw Cen MT Condensed";    

        //create a textfield for question information

        addChild ( question );

        question.setTextFormat( tf );

        question.multiline = true;

        question.width = question.textWidth;

        question.autoSize = TextFieldAutoSize.LEFT;

        question.defaultTextFormat = tf;

        question.text = quiz.question[currentQuestion].description;

        for ( var a = 0; a < 4; a++ )           
        {

            var answer = new TabAnswer();

            answers.push( answer );

            addChild ( answers[a] );

            answers[a].x = question.x;

            answers[a].y = question.y  + question.height + answer.height + ( a * answer.height * 1.5 );

            answers[a].answer.text = quiz.question[currentQuestion].answers.answer[a];

            answers[a].addEventListener ( MouseEvent.MOUSE_DOWN, answerSelected );

        }

        addChild( next );

        next.x = stage.stageWidth - next.width * 1.3;

        next.y = stage.stageHeight - next.height * 2.3;

        next.addEventListener( MouseEvent.MOUSE_DOWN, nextQuestion );

    }

    private function answerSelected ( e:MouseEvent ):void
    {


        trace ( e.currentTarget.answer.text );




        //check to see if the answer that has been pressed, matches the
        //correctAnswer node in the quiz xml question

    }





    private function nextQuestion ( e:MouseEvent ):void
    {

        currentQuestion++;

        getQuestion( currentQuestion );


    }

    private function getQuestion( questionToGet:int ):void
    {

        question.text = quiz.question[questionToGet].description;

        for ( var a = 0; a < 4; a++ )           
        {                           

            answers[a].answer.text = quiz.question[questionToGet].answers.answer[a];

        }

    }

}

}

Aucun commentaire:

Enregistrer un commentaire