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