Skip to main content
Known Participant
April 19, 2013
Question

Can an index from one array, be joined with an index in a different array in response to, or followi

  • April 19, 2013
  • 2 replies
  • 1212 views

Can an index from one array, be joined with an index in a different array in response to, or following, a keyboard event?

I have two arrays, Array 1 (cvcwords ), displays a series of words, each time the Enter key is pressed, a new word is presented.

[When the pupil fails to read the word correctly the tester hits the SPACE Key. This brings up a text input box for the tester to type in the pupil’s incorrect response to the word presented]   Array 2 (cvctextinputarray ),contains a series of functions. Each of these functions is directly related to each of the words from Array 1. For example if the word lap is presented and the pupil says lip, The tester hits the SPACE key which should bring the pupilsResponseLIP() function into action showing an input text box to receive the pupil’s incorrect response. Otherwise the tester would hit the ENTER key and a correct response would be recorded and a new word displayed.

No matter what way I do this I am unable to get the correct function from Array 2 to display with the appropriate word from Array 1. They need to be linked so that the wrong function does not get linked to the wrong word.

To help with this I have coded the text boxes to show whether they are the correct input box for the correct word when I run the program. Can anybody help with this? I include the main bits of code below;

public class ActivityScreen extends SceneBase {

          private var textfield = String;

          private var cvcwords : Array;

private var myTextIN : String = "Type pupil's response to IN";

private var myTextAT : String = "Type pupil's response to AT";

private var myTextOR : String = "Type pupil's response to OR";

private var myTextLAP : String = "Type pupil's response to LAP";

     var cvcwords : Array = ["in", "at", "or", "lap", "ut", "rot", "get","hug","dam","fix","pot",];

               var cvctextinputarray : Array = [pupilsResponseIN, pupilsResponseAT, pupilsResponseOR, pupilsResponseLAP, pupilsResponseUT, pupilsResponseROT, pupilsResponseGET, pupilsResponseHUG, pupilsResponseDAM, pupilsResponseFIX, pupilsResponsePOT,];

               stage.addEventListener(KeyboardEvent.KEY_DOWN, arrayKeyDown);

               function arrayKeyDown(e : KeyboardEvent) : void {

                     if (e.keyCode == Keyboard.ENTER) {

                          myText.text=cvcwords[counter];

                          addChild(myText);

                          counter++;

                          removeChild(myInputBox);//This will remove any textbox from previous word error inputs

                    }

                     else if (e.keyCode == Keyboard.SPACE ) {

                          cvctextinputarray[counter].call();

                          counter++

                     }

               }

function pupilsResponseIN() : void {

                     myInputBox;

                     TextInput;

                     myInputBox.text = myTextIN;

                     addChild(myInputBox);

               }

               function pupilsResponseAT() : void {

                     myInputBox;

                     TextInput;

                     myInputBox.text = myTextAT;

                     addChild(myInputBox);

               }

               function pupilsResponseOR() : void {

                     myInputBox;

                     TextInput;

                     myInputBox.text = myTextOR;

                     addChild(myInputBox);

               }

               function pupilsResponseLAP() : void {

                     myInputBox;

                     TextInput;

                     myInputBox.text = myTextLAP;

                     addChild(myInputBox);

               }

}

This topic has been closed for replies.

2 replies

Inspiring
April 19, 2013

To begin with, your class is full of wrong syntax. Try this first and see if it remedies at least some issues:

package

{

          import flash.events.Event;

          import flash.events.KeyboardEvent;

          import flash.ui.Keyboard;

 

          public class ActivityScreen extends SceneBase

          {

                    private var textfield = String;

 

                    private var cvcwords:Array;

                    private var myTextIN:String = "Type pupil's response to IN";

                    private var myTextAT:String = "Type pupil's response to AT";

                    private var myTextOR:String = "Type pupil's response to OR";

                    private var myTextLAP:String = "Type pupil's response to LAP";

 

                    private var cvcwords:Array = ["in", "at", "or", "lap", "ut", "rot", "get", "hug", "dam", "fix", "pot",];

 

                    private var cvctextinputarray:Array = [pupilsResponseIN, pupilsResponseAT, pupilsResponseOR, pupilsResponseLAP, pupilsResponseUT, pupilsResponseROT, pupilsResponseGET, pupilsResponseHUG, pupilsResponseDAM, pupilsResponseFIX, pupilsResponsePOT,];

 

 

 

                    public function ActivityScreen()

                    {

                              if (stage)

                                        init();

                              else

                                        addEventListener(Event.ADDED_TO_STAGE, init);

                    }

 

                    private function init(e:Event = null):void

                    {

                              removeEventListener(Event.ADDED_TO_STAGE, init);

                              stage.addEventListener(KeyboardEvent.KEY_DOWN, arrayKeyDown);

                    }

 

                    private function arrayKeyDown(e:KeyboardEvent):void

                    {

                              if (e.keyCode == Keyboard.ENTER)

                              {

                                        myText.text = cvcwords[counter];

                                        addChild(myText);

                                        counter++;

                                        removeChild(myInputBox); //This will remove any textbox from previous word error inputs

 

                              }

                              else if (e.keyCode == Keyboard.SPACE)

                              {

                                        cvctextinputarray[counter].call();

                                        counter++

                              }

 

                    }

 

                    private function pupilsResponseIN():void

                    {

                              myInputBox.text = myTextIN;

                              addChild(myInputBox);

                    }

 

                    private function pupilsResponseAT():void

                    {

                              myInputBox.text = myTextAT;

                              addChild(myInputBox);

                    }

 

                    private function pupilsResponseOR():void

                    {

                              myInputBox.text = myTextOR;

                              addChild(myInputBox);

                    }

 

                    private function pupilsResponseLAP():void

                    {

                              myInputBox.text = myTextLAP;

                              addChild(myInputBox);

                    }

          }

}

Known Participant
April 19, 2013

Thank you Andrei I have only included some of the code. But your help is appreciated and I will tidy up my code. As it is though, the program actually works but what is not working properly is, that the textinput boxes are not corresponding with the word presented. I need to have the correct input box with the correct word. The text input box is only called up when the pupil has made an error, and the tester hits the SPACE key. The tester will then type in the pupil's incorrect response for later analysis. How the program works is as follows,  lets say for example, the pupil sees the word "in" presented, but says "on", the tester wll then hit the space key, type in the pupil's response ("on"), into the textbox which should appear. Then the tester hits the ENTER key again and a new word is presented and the input box disappears and the test continues on.

Inspiring
April 20, 2013

I sort of start understanding what you are trying to do but I sense that the very approach of linking functions is extremely cumbersome. It could be done in a much more simpler way. But, again, I cannot make concrete suggestions until I understand the flow.

Can you describe required use cases again as if you did not write the code yet?

Inspiring
April 19, 2013

Not sure if I fully understand your problem but here is a simple example how to convert Strings to functions:

import flash.events.MouseEvent;

var arr:Array = ["f0"];

function f0():void

{

    trace("FUNCTION 0 was executed");

}

btn.addEventListener(MouseEvent.CLICK, functionHandler);

//this grabs the contents of the the input-text and looks if there is a function available with the same name

function functionHandler(e:MouseEvent):void

{

    //check if function available

   

    if (arr.indexOf(input_txt.text)!=-1)

    {

      

        this[input_txt.text]();

    }

    else

    {

        trace("This is no valid function");

    }

}

Known Participant
April 19, 2013

Thanks for that However Im not trying to convert strings to functions. Perhaps my discription may have been confusing. The fact that one array contains text and the other contains functions is not really the issue, except that the function will carry out other work later on which is not relevant right now. My problem for now is; "When the tester hits the SPACE key, How do I get the correct textinput box to pop up ready to collect the pupils response". The program works ok, but what happens is the wrong textinput boxes are presented with the wrong words. [They are subject to the counter++ as well. So the text box which should pop up to receive the input for the word "in", will actually be the textinput box for the word "or"]. I need to know how they can be coordinated so that the textinput box for "in" will appear on the same screen at the same time as the word "in" if the SPACE bar has been hit.