33 Replies Latest reply on Sep 23, 2012 9:59 AM by Andrei1

    Newbie: Comparing Arrays

    donalall Level 2

      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);

      }

       

      }

      }

        • 1. Re: Newbie: Comparing Arrays
          kglad Adobe Community Professional & MVP

          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[i].toLowerCase()==userInputA[i].toLowerCase()){

          score++;

          }

          }

          return score;

          }

          1 person found this helpful
          • 2. Re: Newbie: Comparing Arrays
            donalall Level 2

            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

            • 3. Re: Newbie: Comparing Arrays
              kglad Adobe Community Professional & MVP

              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.

              • 4. Re: Newbie: Comparing Arrays
                donalall Level 2

                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?

                • 5. Re: Newbie: Comparing Arrays
                  kglad Adobe Community Professional & MVP

                  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

                  }

                  1 person found this helpful
                  • 6. Re: Newbie: Comparing Arrays
                    donalall Level 2

                    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_);

                    }

                    • 7. Re: Newbie: Comparing Arrays
                      kglad Adobe Community Professional & MVP

                      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]);

                      }

                      1 person found this helpful
                      • 8. Re: Newbie: Comparing Arrays
                        donalall Level 2

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

                         

                        Error #1056: Cannot create property tf_0.

                        • 9. Re: Newbie: Comparing Arrays
                          kglad Adobe Community Professional & MVP

                          copy and paste the code you used.

                          • 10. Re: Newbie: Comparing Arrays
                            donalall Level 2

                            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]);

                            }

                            • 11. Re: Newbie: Comparing Arrays
                              kglad Adobe Community Professional & MVP

                              make your sprite sublclass dynamic or extend the movieclip class.

                               

                              p.s. please mark helpful/correct responses.

                              • 12. Re: Newbie: Comparing Arrays
                                donalall Level 2

                                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

                                • 13. Re: Newbie: Comparing Arrays
                                  kglad Adobe Community Professional & MVP

                                  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
                                  1 person found this helpful
                                  • 14. Re: Newbie: Comparing Arrays
                                    donalall Level 2

                                    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.

                                    • 15. Re: Newbie: Comparing Arrays
                                      kglad Adobe Community Professional & MVP

                                      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.

                                      • 16. Re: Newbie: Comparing Arrays
                                        donalall Level 2

                                        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.

                                        • 17. Re: Newbie: Comparing Arrays
                                          kglad Adobe Community Professional & MVP

                                          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);

                                          • 18. Re: Newbie: Comparing Arrays
                                            donalall Level 2

                                            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 = [];

                                            • 19. Re: Newbie: Comparing Arrays
                                              kglad Adobe Community Professional & MVP

                                              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?

                                              • 20. Re: Newbie: Comparing Arrays
                                                donalall Level 2

                                                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()

                                                • 21. Re: Newbie: Comparing Arrays
                                                  kglad Adobe Community Professional & MVP

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

                                                  • 22. Re: Newbie: Comparing Arrays
                                                    Andrei1 Level 6

                                                    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[i], answer: answer[i] } );
                                                                }
                                                            }
                                                            /**
                                                             * 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);
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                    
                                                    • 23. Re: Newbie: Comparing Arrays
                                                      donalall Level 2

                                                      Hi Andrei1,

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