Skip navigation
Currently Being Moderated

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

Apr 6, 2013 11:45 AM

Tags: #actionscript #flash_professional

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

 

                        }

                              }

 


 
Replies
  • Currently Being Moderated
    Apr 6, 2013 12:02 PM   in reply to Domhnall Ó Suibhne

    You don't need to add/remove the textfield, just assigning the text is sufficient.

     

    What you need to do is create a variable to use as an incrmented index to go thru the array.  It starts at 0 and gets incremented by 1 each time your keypress function executes.  Use that index value in the line...

     

    myText.text = cvcwords[1]; 

     

    as in...

     

    myText.text = cvcwords[counter];     // show the current text

    counter += 1;                                    // increment it for the next time

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 6, 2013 5:49 PM   in reply to Domhnall Ó Suibhne

    I would recommend you have just the one listener and one event handler function since the same keys get processed in both

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 6, 2013 6:02 PM   in reply to Domhnall Ó Suibhne

    Based on what I see of your code, the following is all you might need...

     

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


    stage.addEventListener(KeyboardEvent.KEY_DOWN, modKeyDown);

     

    function modKeyDown(e : KeyboardEvent) : void {
        if (e.keyCode == Keyboard.ENTER) {
            myText.text = cvcwords[counter];      

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

            myText.text = "try again";
            addChild(myText);
            stage.removeEventListener(KeyboardEvent.KEY_DOWN, modKeyDown);
        }
    }

     

     

    When you test this in Flash, in the player you need to be sure to select the Diable Keyboard Shortcuts option found under the Control option in the top toolbar

     

    If you really mean to have them try again, you probably do not want to remove the event listener

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 7, 2013 7:43 AM   in reply to Domhnall Ó Suibhne

    You're welcome

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points