7 Replies Latest reply on Apr 7, 2013 7:43 AM by Ned Murphy

    to display consecutive words from an array on a keypress. Event listeners not working.

    Domhnall Ó Suibhne

      I need to display a series of words consecutively on a black background. A new word presented each time the Enter key is pressed (It is necessary that its the same key each time i.e the Enter Key). The words are arranged in an array. I have created a function for each Keypress and a listener. Also, as the previous word is replaced by the next, I have a removeChild() to get rid of the the last word.

      I have a removeEventListener set up also, to avoid any problems with the listeners. I have been at this a long time without sucess. It must be a listener problem (i.e I have not got the listeners focussed properly?) or a display problem. It only ever displays one word. Please see below code for the display of two of the words from the array which is called cvcwords. The main problem is that the words do not display one after the other (indeed not at all, except for the first one) when the enter key is pressed. Very much as a powerpoint presentation would, thats the idea anyway. I have just a sample of the code below. any help appreciated.

       

      var cvcwords : Array = ["bad", "mod", "hud", "mit", "sat", "fog", "puc",];

       

       

       

      this.stage.addEventListener(KeyboardEvent.KEY_DOWN, modKeyDown);

       

                                    function modKeyDown(e : KeyboardEvent) : void {

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

                                                        removeChild(myText);               // to remove previous word from screen

       

                                                        myText.text = cvcwords[1];        //to display element one of the array

                                                        addChild(myText);

                                              } else if (e.keyCode == Keyboard.SPACE) { //this is for another seperate option

                                                        myText.text = "try again";

                                                        addChild(myText);

       

                     this.stage.removeEventListener(KeyboardEvent.KEY_DOWN, modKeyDown);

       

      }

                                    }

                                    this.stage.addEventListener(KeyboardEvent.KEY_DOWN, hudKeyDown);

       

                                    function hudKeyDown(e : KeyboardEvent) : void {

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

                                                        removeChild(myText);  //to remove previous word from the screen      

       

                                                        myText.text = cvcwords[2];

                                                        addChild(myText);

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

                                                        myText.text = "do again";

                                                        addChild(myText);

       

                     this.stage.removeEventListener(KeyboardEvent.KEY_DOWN, hudKeyDown);

       

                              }

                                    }