ok so I have a method to shoot a spell, and whenever I press space it does fire, however if I push space while it's still on the screen my spell's speed doubles and it starts to repeat itself without me pushing space again here's my code thanks :) oh and the WizardCells is just a 2D array of JLabels I know that's created and used properly so I didn't want to waste your guys' time with that code thanks :)
public void shootSpell(){
final BlueSpell b = new BlueSpell(GoodGuy.getx(), GoodGuy.gety() +1, BlueSpellWizard());
int delay = 60;
ActionListener timeListen = new ActionListener(){
public void actionPerformed(ActionEvent e){
if(b.gety() != 19){
WizardCells[b.getx()][b.gety()].setIcon(null);
WizardCells[b.getx()][b.changey(b.gety()+1)].setIcon(b.getIcon());
}
else{
WizardCells[b.getx()][b.gety()].setIcon(null);
b.changex(GoodGuy.getx());
b.changey(GoodGuy.gety() +1);
timer.stop();
}
}
};
timer = new Timer(delay, timeListen);
if(timer.isRunning()){
return;
}
else{
timer.start();
}
}
Key handler:
public class WizardKeyHandeler extends WizardBattleGrid implements KeyListener {
GoodGuy Player = new GoodGuy(10, 0, GoodGuyWizardIcon());
BlueSpell GoodSpell = new BlueSpell(10, 1, BlueSpellWizard());
WizardPause pause = new WizardPause();
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_W){
if(Player.getx() != 0){
Player.moveUp();
}
}
else if(key == KeyEvent.VK_S){
if(Player.getx() != 19){
Player.moveDown();
}
}
else if(key == KeyEvent.VK_D){
if(Player.gety() != 9){
Player.moveRight();
}
}
else if(key == KeyEvent.VK_A){
if(Player.gety() != 0){
Player.moveLeft();
}
}
else if(key == KeyEvent.VK_SPACE){
}
else if(key == KeyEvent.VK_ESCAPE){
pause.createPause();
}
}
@Override
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_W){
if(Player.getx() != 0){
Player.remainAtPosition();
}
}
else if(key == KeyEvent.VK_S){
if(Player.getx() != 19){
Player.remainAtPosition();
}
}
else if(key == KeyEvent.VK_D){
if(Player.gety() != 9){
Player.remainAtPosition();
}
}
else if(key == KeyEvent.VK_A){
if(Player.gety() != 0){
Player.remainAtPosition();
}
}
else if(key == KeyEvent.VK_SPACE){
GoodSpell.shootSpell();
}
else if(key == KeyEvent.VK_ESCAPE){
pause.createPause();
}
}
@Override
public void keyTyped(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_W){
if(Player.getx() != 0){
Player.moveUp();
}
}
else if(key == KeyEvent.VK_S){
if(Player.getx() != 19){
Player.moveDown();
}
}
else if(key == KeyEvent.VK_D){
if(Player.gety() != 9){
Player.moveRight();
}
}
else if(key == KeyEvent.VK_A){
if(Player.gety() != 0){
Player.moveLeft();
}
}
else if(key == KeyEvent.VK_SPACE){
}
else if(key == KeyEvent.VK_ESCAPE){
pause.createPause();
}
}