I'm implementing in Java for making a battle menu in an RPG, and I found this problem that I can't manage to fix. When I press my <Z> key I use like an accept to enter the magic SubMenu, it considers that press to also select a spell in the menu, not only to open it, I tried debugging or using booleans to stop it from malfunctioning like that but I can't find the reason, thanks in advance and hope you have a great day.
Photo of the menu to understand it better: Image from the Menu and Submenu
Code From the KeyPressed Event:
public void combatState(int code) {
if (code == KeyEvent.VK_W) {
if (gp.ui.commandNum > 0 && !gp.ui.magicMenu) {
gp.ui.commandNum--;
System.out.println(gp.ui.commandNum);
} else if (!gp.ui.magicMenu) {
gp.ui.commandNum = 4;
}
}
if (code == KeyEvent.VK_S) {
if (gp.ui.commandNum < 4 && !gp.ui.magicMenu) {
gp.ui.commandNum++;
System.out.println(gp.ui.commandNum);
} else if (!gp.ui.magicMenu) {
gp.ui.commandNum = 0;
}
}
if (code == KeyEvent.VK_Z) {
if (gp.ui.commandNum == 0) {
gp.battleSystem.attack();
}
if (gp.ui.commandNum == 1) {
gp.ui.magicMenu = true;
}
if (gp.ui.commandNum == 2) {
// Use Items Aun sin Implementar
}
if (gp.ui.commandNum == 3) {
gp.battleSystem.defend();
}
if (gp.ui.commandNum == 4) {
// Flee Aun sin Implementar
}
}
// Aquí obtenemos la cantidad de hechizos disponibles
int numSpells = gp.player.numberOfSpells();
if (gp.ui.commandNum == 1 && gp.ui.magicMenu) {
if (code == KeyEvent.VK_W) {
if (gp.ui.commandNum2 > 0) {
gp.ui.commandNum2--;
System.out.println(gp.ui.commandNum2);
} else {
gp.ui.commandNum2 = numSpells - 1; // Ir al último hechizo
}
}
if (code == KeyEvent.VK_S) {
if (gp.ui.commandNum2 < numSpells - 1) {
gp.ui.commandNum2++;
System.out.println(gp.ui.commandNum2);
} else {
gp.ui.commandNum2 = 0; // Volver al primer hechizo
}
}
// Verificar si estamos dentro del submenu de magia antes de activar el hechizo
if (code == KeyEvent.VK_Z) {
// Aquí activa el hechizo seleccionado (sin pasar una variable)
gp.battleSystem.useMagic();
}
if(code == KeyEvent.VK_ESCAPE){
gp.ui.commandNum2 = 0;
gp.ui.magicMenu = false;
}
}
}
Code from the useMagic():
public void useMagic() {
// Obtener el hechizo seleccionado por el jugador
ArrayList<superMagic> playerSpells = player.getSpells();
if (gp.ui.commandNum2 >= 0 && gp.ui.commandNum2 < playerSpells.size()) {
superMagic selectedSpell = playerSpells.get(gp.ui.commandNum2);
// Verificar si el jugador tiene suficiente MP para lanzar el hechizo
if (player.PLAYERstats.mp >= selectedSpell.mpCost) {
// Restar el costo de MP al jugador
player.PLAYERstats.mp -= selectedSpell.mpCost;
// Realizar cálculos de daño o efectos del hechizo según sea necesario
int damage = selectedSpell.damage;
// Puedes agregar lógica adicional aquí para diferentes tipos de hechizos
// Aplicar los efectos del hechizo al enemigo (monstruo)
if (turn == 0) {
monster.health -= damage;
System.out.println(monster.name + " has recived " + damage + " damage from " + selectedSpell.name);
}
// Aplicar los efectos del hechizo al jugador
else if (turn == 1) {
player.PLAYERstats.hp -= damage;
System.out.println("Player has recived " + damage + " damage from " + selectedSpell.name);
}
// Actualizar la interfaz de usuario para reflejar los cambios
// Puedes agregar código aquí para mostrar mensajes o actualizaciones visuales
// Cambiar al siguiente turno
nextTurn();
} else {
System.out.println("Not enough MP to cast " + selectedSpell.name);
}
}
}
I'm a bit new on making this posts so if I miss something important let me now so I update the post.
I've tried creating boolean variables to confirm if you are in the submenu first before selecting the spell but still insta uses it and debugging multiple times to see if it is considered like pressing more times but it is only considered as pressed 1.
Also tried with a
thread.sleep()that was a stupid try that of course didn't work.