Just noticed a problem in my code, when I use the same key to take input from two menus, it will re-use the input again for the next one.
Example: In my program, pressing enter opens instructions, but in the instructions page, enter goes back to the menu. Result is that it appears as if the instructions aren't coming up at all.
I am using a different key listener for the main menu and for the instructions screen, and I remove it when I switch to the other.
Here's the relevant code -
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| public void keyPressed(KeyEvent e) {
if (mainMenu) { key = e.getKeyCode(); if (key == KeyEvent.VK_UP) { if (playIsSelected) { playIsSelected = false; quitIsSelected = true; } else if (insIsSelected) { insIsSelected = false; playIsSelected = true; } else if (quitIsSelected) { quitIsSelected = false; insIsSelected = true; } } if (key == KeyEvent.VK_DOWN) { if (playIsSelected) { playIsSelected = false; insIsSelected = true; } else if (insIsSelected) { insIsSelected = false; quitIsSelected = true; } else if (quitIsSelected) { quitIsSelected = false; playIsSelected = true; } } if (key == KeyEvent.VK_ENTER) { if (playIsSelected) gameInit(); if (insIsSelected) instructionsInit(); if (quitIsSelected) System.exit(0); } } if (playing) { p.keyPressed(e); } if (instructions) { key = e.getKeyCode(); if (key == KeyEvent.VK_ENTER) menuInit(); } } |
Thanks,
-Nathan