• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Newbie: Comparing Arrays

Engaged ,
Sep 12, 2012 Sep 12, 2012

Copy link to clipboard

Copied

Hi,

I want to create a screen (using just AS3 code) with a number of user input questions on it.

e.g. Question stem: What is the capital of France?   Answer: Paris.

I have the code for displaying the "question stem" and the "text entry box".

But I do not know what code to use to compare the two arrays:

Array 1: correct answer, Array 2: user input answer.

User will click a Submit button once they have answered all the questions. i.e. One Submit button.

I would also like the text "Correct" or "Incorrect" to appear to the right of the text entry box.

I'm new to AS3 so any changes to the code i've provided would be welcome.

Below is the code for 3 questions. (without the Submit button code)

package

{

import flash.display.Sprite;

import flash.text.TextField;

import flash.text.TextFormat;

public class Main_questions extends Sprite

{

// DEFAULT TEXT FORMAT

var textFormat:TextFormat = new TextFormat("verdana", 9)

public function Main_questions():void

// QUESTIONS IN A LOOP       

var Q_stem_array:Array = new Array();

           

for(var i:int = 0; i < 3; i++) 

{

var Question:TextField = new TextField();

Question.defaultTextFormat = textFormat;

Question.border = false;

Question.width = 300;

Question.height = 20;

Question.x = 30;

Question.y = i * 35 + 70;

Q_stem_array.push(Question);

               

addChild(Question);

}

Q_stem_array[0].text = "1. What is the Capital of Ireland?"; 

Q_stem_array[1].text = "2. What is the Capital of France?";  

Q_stem_array[2].text = "3. What is the Capital of Spain?";

// TEXT ENTRY BOXES (TEBs) IN A LOOP

for(var i:int = 0; i < 3; i++)

{

var TEBfield:TextField = new TextField();

TEBfield.defaultTextFormat = textFormat;

TEBfield.border = true;

TEBfield.width = 200;

TEBfield.height = 20;

TEBfield.x = 200;

TEBfield.y = i * 35 + 70;

TEBfield.type = "input"; 

textArray.push(TEBfield);

                          

addChild(TEBfield);

}

}

}

TOPICS
ActionScript

Views

5.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Sep 15, 2012 Sep 15, 2012

Conceptually, this kind of applications should have entities that supplies data (data provider) and links visuals to the data.

In your case data is questions/answers while visuals are TextField instances.

I would strongly advise against using dynamic classes (that allows for adding properties dynamically at runtime via associative array notation), especially in this kind of cases. While utilizing dynamic features sometimes is useful, in the majority of cases it present major headaches down the roa

...

Votes

Translate

Translate
Community Expert ,
Sep 12, 2012 Sep 12, 2012

Copy link to clipboard

Copied

do you know how to create and populate the two (correct answers and user input) arrays?

if yes, loop through one of them and compare their elements:

function scoreF():int{

var score:int = 0;

for(var i:int=0;i<correctAnswerA.length;i++){

if(correctAnswerA.toLowerCase()==userInputA.toLowerCase()){

score++;

}

}

return score;

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 13, 2012 Sep 13, 2012

Copy link to clipboard

Copied

I know how to create and populate the "Correct Answers". i.e.

    var Answers:Array = new Array("Dublin", "Paris", "Madrid");

But don't know how to create and populate the "User input answers" array.

Also where would i put the function "scoreF" you provided?

Would it be in the "buttonClick Handler".

i.e.   private function buttonClickHandler(event:MouseEvent):void

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 13, 2012 Sep 13, 2012

Copy link to clipboard

Copied

whereever you detect that the user has completed their answer to the current question, use:

userInputA.push(yourinputtextfield.text);

and you call scoreF() when the quiz is completed and you want to display the user's score.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 13, 2012 Sep 13, 2012

Copy link to clipboard

Copied

I want the user to answer all questions on the screen. Then click the Submit button to be assessed on whether they got each question correct or incorrect.

So not sure if your "push" code handles this or assesses just one question at a time?

Also how is the "user input array" created? Do i have to have a "var ...:Array = new Array.." somewhere?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 13, 2012 Sep 13, 2012

Copy link to clipboard

Copied

yes, you need:

var userInputA:Array=[];

to declare and define an array.

when the submit button is clicked (eg, in your click listener), you can use:

for(var i:int=1;i<=n;i++){

userInputA.push(this["tf_"+i].text);  // where your input textfield for question 1 is tf_1, for question 2 is tf_2 etc

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 13, 2012 Sep 13, 2012

Copy link to clipboard

Copied

Your "for" statement within "Click listener" displayed an output error message.

ReferenceError: Error #1069: Property TEBfield1 not found on Main_Sept13 and there is no default value.     at Main_Sept13/buttonClickHandler() )

I'm assuming i have to assign a number (e.g. tf_1, tf_2 etc) to a "input textfield" when i create it

Below is the looped "input textfield" code i have. Also should the "var tf_..." line be placed here?

for(var i:int = 0; i < 3; i++)          // Text Field Boxes (tf_) in a Loop

{

var tf_:TextField = new TextField();

tf_.defaultTextFormat = textFormat;

tf_.border = true;

tf_.width = 200;

tf_.height = 20;

tf_.x = 200;

tf_.y = i * 35 + 70;

tf_.type = "input"; 

addChild(tf_);

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 13, 2012 Sep 13, 2012

Copy link to clipboard

Copied

you need to create those textfields.  if you're using code to create them, use:

for(var i:int = 0; i < 3; i++)          // Text Field Boxes (tf_) in a Loop

{

this["tf_"+i]= new TextField();

this["tf_"+i].defaultTextFormat = textFormat;

this["tf_"+i].border = true;

this["tf_"+i].width = 200;

this["tf_"+i].height = 20;

this["tf_"+i].x = 200;

this["tf_"+i].y = i * 35 + 70;

this["tf_"+i].type = "input"; 

addChild(this["tf_"+i]);

}

or save yourself some typing and use:

for(var i:int = 0; i < 3; i++)          // Text Field Boxes (tf_) in a Loop

{

this["tf_"+i]= new TextField();

with(this["tf_"+i]){

defaultTextFormat = textFormat;

border = true;

width = 200;

height = 20;

x = 200;

y = i * 35 + 70;

type = "input";

addChild(this["tf_"+i]);

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 14, 2012 Sep 14, 2012

Copy link to clipboard

Copied

I copied and pasted your code above for creating the textfields and it gave me this error.

Error #1056: Cannot create property tf_0.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 14, 2012 Sep 14, 2012

Copy link to clipboard

Copied

copy and paste the code you used.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 14, 2012 Sep 14, 2012

Copy link to clipboard

Copied

I tried both bits of code you provided.

For example - here is the code i used - your second example.

for(var i:int = 0; i < 3; i++)      // Text Field Boxes (tf_) in a Loop

{

this["tf_"+i]= new TextField();

with(this["tf_"+i]){

defaultTextFormat = textFormat;

border = true;

width = 200;

height = 20;

x = 200;

y = i * 35 + 70;

type = "input";

}

addChild(this["tf_"+i]);

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 14, 2012 Sep 14, 2012

Copy link to clipboard

Copied

make your sprite sublclass dynamic or extend the movieclip class.

p.s. please mark helpful/correct responses.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 14, 2012 Sep 14, 2012

Copy link to clipboard

Copied

When you say "extend the movieclip class" do you mean:

public class Main_Sept13 extends MovieClip

(Wasn't sure how to make the Sprite subclass dynamic so I changed the Sprites to MovieClips)

It still brings up the same error.

Error #1056: Cannot create property tf_0

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 14, 2012 Sep 14, 2012

Copy link to clipboard

Copied

you'll have to make that dynamic, too:


public dynamic class Main_Sept13 extends MovieClip

or, if you want to use sprite:


public class Main_Sept13 extends Sprite

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 14, 2012 Sep 14, 2012

Copy link to clipboard

Copied

Thanks - Dynamic Movieclip worked.

But when i click the Submit button after the input fields are filled in i get this error.

Error #1010: A term is undefined and has no properties.

    at MethodInfo-2()

Here is the "Click Handler" code: 

// Click Submit Button

function buttonClickHandler(event:MouseEvent):void

{

for(var i:int=0;i<=3;i++){

UserInput.push(this["tf_"+i].text);

UserInput defined at the top after the class =  var UserInput:Array = [];

"tf_" defined in the For loop for the Textinput fields - code you provided above.

(i'm assuming the problem relates to "tf_"?)

Once i get this working i'll also need to add the Score function you provided previously.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 14, 2012 Sep 14, 2012

Copy link to clipboard

Copied

if you only created 3 textfields you shouldn't be checking 4.  use:

function buttonClickHandler(event:MouseEvent):void

{

for(var i:int=0;i<3;i++){

UserInput.push(this["tf_"+i].text);

p.s.  you should start marking the helpful/correct responses so others can quickly find them.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 14, 2012 Sep 14, 2012

Copy link to clipboard

Copied

Still the same error with the Push code.

{

for(var i:int=0;i<3;i++){

UserInput.push(this["tf_"+i].text);

TypeError: Error #1010: A term is undefined and has no properties.

    at MethodInfo-1()

p.s. I have now marked the helpful responses.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 14, 2012 Sep 14, 2012

Copy link to clipboard

Copied

is that code in your Main_questions class?  is UserInput defined AND initialized.  if yes, yes and yes, what does the following show:

{

for(var i:int=0;i<3;i++){

trace(UserInput.length,this)

trace(i,this["tf"_"+i])

UserInput.push(this["tf_"+i].text);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 14, 2012 Sep 14, 2012

Copy link to clipboard

Copied

As mentioned previously:

1. The Push code is the Click handler function - only two functions: Main, ClickHandler.

(User clicks Submit button to check answers)

function.buttonClickHandler(event:MouseEvent):void

2. UserInput is defined in the Main class:

var UserInput:Array = [];

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 14, 2012 Sep 14, 2012

Copy link to clipboard

Copied

i don't know what you're calling your Main class.

but all the code you've been showing should be in the same class or you have a scope problem.

if all your code is in the same class, what do those trace() functions reveal?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 14, 2012 Sep 14, 2012

Copy link to clipboard

Copied

My class is called "Main_questions".

    public dynamic class Main_questions extends MovieClip

The class consists of two functions:

1. public function Main_questions():void

2. function buttonClickHandler(event:MouseEvent):void

UserInput is defined in the "Main_questions" class:

      var UserInput:Array = [];

The trace functions:

(trace(UserInput.length,this)

trace(i,this["tf"_"+i])


Return these:

0 [object global]

0 undefined

TypeError: Error #1010: A term is undefined and has no properties.

    at MethodInfo-1()

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 14, 2012 Sep 14, 2012

Copy link to clipboard

Copied

you have more than one problem with that setup.  copy and paste your updated Main_questions class code.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 15, 2012 Sep 15, 2012

Copy link to clipboard

Copied

Conceptually, this kind of applications should have entities that supplies data (data provider) and links visuals to the data.

In your case data is questions/answers while visuals are TextField instances.

I would strongly advise against using dynamic classes (that allows for adding properties dynamically at runtime via associative array notation), especially in this kind of cases. While utilizing dynamic features sometimes is useful, in the majority of cases it present major headaches down the road and is very inefficient from performance standpoint.

Below is a fully functional class that accomplishes what you are looking for. Role of data provider is accomplished by the questions array. This is an array of objects that accomplish the linkages between questions, answers and TextFields at both configuration and user interaction phases. Read comments.

All features are dynamic except I use name “checkButton” for the submit button.

Theoretically this application asks for at least two more classes but, because you stated that you are new to AS3 – this is a sufficient beginning.

At last, you should get into a habit to declare as few variables as possible and NEVER declare variables in the loops. Declaring variables inside loop bodies is a pretty bad practice.

package

{

    import flash.display.Sprite;

    import flash.events.MouseEvent;

    import flash.text.TextField;

    import flash.text.TextFieldAutoSize;

    import flash.text.TextFieldType;

    import flash.text.TextFormat;

   

    public class Main_questions extends Sprite

    {

        // use single object that acts as a data provider and links questions, answers and runtime object

        private var questions:Array;

        // DEFAULT TEXT FORMAT

        private var textFormat:TextFormat = new TextFormat("Verdana", 9);

        // correct feedback format

        private var correctTF:TextFormat = new TextFormat("Verdana", 9, 0x008000);

        // incorrect feedback format

        private var incorrectTF:TextFormat = new TextFormat("Verdana", 9, 0xDF0000);

       

        public function Main_questions():void

        {

            init();

        }

       

        private function init():void

        {

            initData();

            drawQuestions();

        }

        /**

         * This method inititate data and populates it with questions/answers

         */

        private function initData():void

        {

            questions = [];

            // questions

            var question:Array = ["What is the Capital of Ireland?", "What is the Capital of France?", "What is the Capital of Spain?"];

            // answers

            var answer:Array = ["Dublin", "Paris", "Madrid"];

            for (var i:int = 0; i < question.length; i++) {

                // push new object into questions array - this uses literal object instantiation notation {}

                questions.push( { question: question, answer: answer } );

            }

        }

        /**

         * This method draws text fields and adds them to their correcponding objects

         */

        private function drawQuestions():void

        {

            // use/recycle the same variable - it is more efficient

            var textField:TextField;

            // var to control x positions

            var nextX:Number = 30;

            // var to control y position

            var nextY:Number = 0;

            // loop through questions and create textfileds

            // for each loop is faster than for loop

            // on each loop iteration object element is used from appropriate position on the array

            for each(var q:Object in questions) {

                // CREATE QUESTION TextField

                textField = new TextField();

                textField.defaultTextFormat = textFormat;

                textField.width = 300;

                textField.height = 20;

                textField.x = nextX;

                textField.y = nextY;

                // get question text from the object in questions array

                // ordinal inserted based on the index of question in the array

                textField.text = (questions.indexOf(q) + 1) + ". " + q.question;

                // change nextX

                nextX += textField.width;

                // add field to display list

                addChild(textField);

                // add text field to the object for future linkage to answer

                // this step is optional because actual answer string will be used

                q.questionText = textField;

                // CREATE ANSWER input field

                // note - the same variable textField is used again - NOT the same instance but different instance of THE SAME variable

                textField = new TextField();

                textField.defaultTextFormat = textFormat;

                textField.border = true;

                textField.width = 200;

                textField.height = 20;

                textField.type = TextFieldType.INPUT;

                textField.x = 30;

                textField.x = nextX;

                textField.y = nextY;

                // change nextX to accomodate feedback TextField

                nextX += textField.width;

                // add field to display list

                addChild(textField);

                // add text field to the object for future linkage to answer

                q.answerText = textField;

                // CREATE FEEDBACK field

                // again - use the same textField variable to create new instances

                textField = new TextField();

                textField.defaultTextFormat = correctTF;

                textField.width = 100;

                textField.height = 20;

                textField.x = nextX + 10;

                textField.y = nextY;

                // add field to display list

                addChild(textField);

                // add feedback field to the object for linkage to question

                q.feedback = textField;

                // reset positions defaults for the next loop iteration

                nextX = 30;

                nextY += textField.height + 10;

               

            }

            // assuming that button name is checkButton

            checkButton.addEventListener(MouseEvent.CLICK, buttonClickHandler);

        }

       

        private function buttonClickHandler(e:MouseEvent):void

        {

            // loop through the objects in questions array and compare answers

            for each(var q:Object in questions) {

                // remove spaces from answers in case user types the in the beginning and the end

                q.answerText.text = String(q.answerText.text).replace(/^\s+|\s+$/, "");

                // compare answers in lower cases

                if (q.answer.toLowerCase() == q.answerText.text.toLowerCase()) {

                    TextField(q.feedback).text = "correct";

                    TextField(q.feedback).setTextFormat(correctTF);

                }

                else {

                    TextField(q.feedback).text = "incorrect!";

                    TextField(q.feedback).setTextFormat(incorrectTF);

                }

            }

        }

    }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 15, 2012 Sep 15, 2012

Copy link to clipboard

Copied

Hi Andrei1,

Yes this is exactly what I was looking for. Thanks a lot!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 16, 2012 Sep 16, 2012

Copy link to clipboard

Copied

While waiting for Godot,  I decided to show a more OOP approach to your application. It may be a bit difficult to digest at first, but if you have time and desire – look into what is happening and how things work together in the classes below.

To test – just put all the classes into the same directory as you FLA and make MainQuestions the document class.

I split functionality into 5 different classes. One of the OOP principals are to make objects very specialized. This approach presents with much better scalability.

Some of the highlights:

  1. Document class became very small and it does only two things – places visuals on display list and monitors how user interacts with the button. Technically even these two aspects shouldbe delegated to another class but from illustration standpoint it is not very relvant.
  2. Data is separated into a special class QuestionsData. It may not be apparent at first but this way you can get data different ways and not change a bit of other code that uses this data. For example, in the future you may want to decide to load data at runtime as XML. Still – your application will function the same way despite the fact that data acquisition, parsing and storage change.
  3. I introduced shuffling capabilities in the QuestionsData class to randomize questions – it feels more natural for quizzes.
  4. QuestionLine class is a display object that contains all visuals that pertain to a single question. Note how data is used. Also this class takes care of user entry validation.
  5. Note that because of QuestionLine class, QuestionsContainer class doesn’t have to deal with specifics of question related visuals in depth. QuestionsContainer just creates QuestionLine instances and redistributes data. See init() method of QuestionsContainer.
  6. QuestionTextFiled class. It feels a touch as an overkill but this class allows for unification of some shared functionality between TextFields that are used in QuestionLine. As a result – there is less coding on QuestionLine level.

There are some other tricks of the trade in the code but they are secondary.

Class MainQuestions - document class

package

{

    import flash.display.Sprite;

    import flash.events.MouseEvent;

   

    public class MainQuestions extends Sprite

    {

       

        // container that displays questions

        private var container:QuestionsContainer;

       

        public function MainQuestions():void

        {

            init();

        }

       

        private function init():void

        {

            // instnatiate container

            container = new QuestionsContainer(new QuestionsData());

            addChild(container);

            container.x = container.y = 30;

            // reposition button to the bottom right

            checkButton.x = container.x + container.width - checkButton.width;

            checkButton.y = container.y + container.height + 10;

           

            checkButton.addEventListener(MouseEvent.CLICK, buttonClickHandler);

        }

       

        private function buttonClickHandler(e:MouseEvent):void

        {

            container.checkAnswers();

        }

   

    }

}

Class QuestionsData

package

{

   

    public class QuestionsData extends Object

    {

        public var questions:Array;

       

        public function QuestionsData()

        {

            init();

        }

       

        private function init():void

        {

            // array of questions

            var q:Array = ["What is the Capital of Ireland?", "What is the Capital of France?", "What is the Capital of Spain?", "What is the Capital of Malaysia?", "What is the Capital of New York?", "What is the Capital of California?"];

            // array of answers

            var a:Array = ["Dublin", "Paris", "Madrid", "Kuala Lumpur", "Albany", "Sacramento"];

            questions = [];

            // populate array with objects that link questions and answers

            for (var i:int = 0; i < q.length; i++)

            {

                questions.push({question: q, answer: a});

            }

            // randomize

            shuffle();

        }

       

        /**

         * Randomizes questions

         */

        public function shuffle():void

        {

            var value:Object;

            var random:int = 0;

            var i:int = questions.length;

            while (i--)

            {

                random = Math.random() * (i + 1);

                value = questions[random];

                questions[random] = questions;

                questions = value;

            }

        }

   

    }

}

Class QuestionLine

package

{

    import flash.display.Sprite;

    import flash.text.TextFieldAutoSize;

    import flash.text.TextFieldType;

    import flash.text.TextFormat;

   

    public class QuestionLine extends Sprite

    {

        // text fields

        private var question:QuestionTextFiled, answer:QuestionTextFiled, feedback:QuestionTextFiled;

        // text formats

        private var correctFormat:TextFormat = new TextFormat("Verdana", 9, 0x008000);

        private var incorrectFormat:TextFormat = new TextFormat("Verdana", 9, 0xDF0000);

        // x gap between fields

        private var gap:Number = 10;

        // related to question data

        private var data:Object;

        // question ordinal

        private var index:int = 0;

        // row background colors

        private var rowColors:Array = [0xE8E8E8, 0xFFFFFF];

        // internal padding

        private var padding:Number = 4;

       

        public function QuestionLine(data:Object, index:int)

        {

            this.data = data;

            this.index = index;

            init();

        }

       

        private function init():void

        {

            question = new QuestionTextFiled();

            answer = new QuestionTextFiled();

            feedback = new QuestionTextFiled();

           

            question.x = padding;

            question.y = answer.y = feedback.y = padding;

           

            answer.width = 150

            feedback.width = 60;

           

            question.autoSize = TextFieldAutoSize.LEFT;

           

            question.text = String(index + 1) + ". " + data.question;

           

            answer.type = TextFieldType.INPUT;

           

            addChild(question);

            addChild(answer);

            addChild(feedback);

        }

       

        /**

         * Provides width of the question field - used for even alignment of

         * textfields in other instances.

         */

        public function get questionWidth():Number

        {

            return question.width;

        }

       

        /**

         * Used to even up fields

         */

        public function set questionWidth(value:Number):void

        {

            // reset autosize so that we can set precise width

            question.autoSize = TextFieldAutoSize.NONE;

            question.width = value;

            // reposition other fields to accomodate passed value

            answer.x = question.x + question.width + gap;

            feedback.x = answer.x + answer.width + gap;

            // draw background based on whether index is odd or even

            graphics.beginFill(rowColors[index % 2]);

            graphics.drawRect(0, 0, width + padding * 2, height + padding * 2);

            // draw buttom line - mainly for the cases when background is white

            // so there is always line that separates the instance visually

            graphics.lineStyle(1, rowColors[1 - index % 2]);

            graphics.moveTo(0, height);

            graphics.lineTo(width, height);

        }

       

        /**

         * Validates user entry and compare entry with the correct answer

         */

        public function checkAnswer():void

        {

            // validate entries

            // remove leading and double spaces

            answer.text = answer.text.replace(/^\s+|\s+$/, "").replace(/\s{2,}/g, " ");

            if (answer.text.toLowerCase() == data.answer.toLowerCase())

            {

                feedback.text = "correct";

                feedback.setTextFormat(correctFormat);

                // make it proper

                answer.text = data.answer;

            }

            else

            {

                feedback.text = "incorrect!";

                feedback.setTextFormat(incorrectFormat);

            }

        }

   

    }

}

Class QuestionsContainer

package

{

    import flash.display.Sprite;

   

    public class QuestionsContainer extends Sprite

    {

        private var data:QuestionsData;

        // array of question lines

        private var qLines:Array;

        // maximum width of question field among all question lines used

        private var maxQWidth:Number = 0;

       

        public function QuestionsContainer(data:QuestionsData)

        {

            this.data = data;

            init();

        }

       

        private function init():void

        {

            // instance of question line that is reused

            var qLine:QuestionLine;

            qLines = [];

           

            for (var i:int = 0; i < data.questions.length; i++)

            {

                qLine = new QuestionLine(data.questions, i);

                // add line to array for further processing

                qLines.push(qLine);

                // grab width if it is the largest one

                maxQWidth = Math.max(maxQWidth, qLine.questionWidth);

            }

            // now that we know the widest line - we can place objects on display list

            placeQuestion();

        }

       

        /**

         * Places question lines on display list and sets largest width for even alignment

         */

        private function placeQuestion():void

        {

            // vertical distance between questions

            var gap = 6;

            // used to set y

            var nextY:Number = 0;

            for each (var q:QuestionLine in qLines)

            {

                addChild(q);

                q.questionWidth = maxQWidth;

                q.y = nextY;

                nextY += q.height;

            }

        }

       

        public function checkAnswers():void

        {

            // loop through the lines and invoke answer checking on them

            for each (var q:QuestionLine in qLines)

                q.checkAnswer();

        }

   

    }

}

Class QuestionTextFiled

package

{

    import flash.text.TextField;

    import flash.text.TextFormat;

   

    public class QuestionTextFiled extends TextField

    {

        private var defaultFormat:TextFormat = new TextFormat("Verdana", 9);

       

        public function QuestionTextFiled()

        {

            init();

        }

       

        private function init():void

        {

            defaultTextFormat = defaultFormat;

            height = Number(defaultFormat.size) * 2;

            multiline = wordWrap = false;

        }

       

        /**

         * Overrides default behavior and adds border/background

         */

        override public function set type(value:String):void

        {

            super.type = value;

            border = true;

            borderColor = 0xC0C0C0;

            background = true;

        }

   

    }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines