i'm just programming a java game and what i want to do is that when i press space, the character goes into fight mode for 5 sec and then release it. i already have done, that i press one time, fight mode gets enabled and when i press another time, its getting released but when i try it like this (code at bottom) the game just freezes for 5 sec but nothing happens and i don't even get a error code.
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JPanel;
public class Zeichnen extends JPanel {
int x = 10;
int y = 335;
int width = 100;
int height = 106;
int speed = 10;
boolean left = false;
boolean fight = false;
String file = "res/character.png";
Image character;
public void paint(Graphics g) {
super.paintComponent(g);
character = Toolkit.getDefaultToolkit().getImage(file);
g.drawImage(character, x, y, width, height, this);
}
public void moveLeft() {
if (left == false) {
left = true;
if (fight == true) {
x = x + 129;
width = -129;
} else if (fight == false) {
x = x + 100;
width = -100;
}
}
x = x - speed;
rand();
repaint();
}
public void moveRight() {
if (left == true) {
left = false;
if (fight == true) {
x = x - 129;
width = 129;
} else if (fight == false) {
x = x - 100;
width = 100;
}
}
x = x + speed;
rand();
repaint();
}
public void moveUp() {
y = y - speed;
repaint();
}
public void moveDown() {
y = y + speed;
repaint();
}
public void fight() {
long curTime = System.currentTimeMillis();
fight = true;
file = "res/character_fight.png";
if (left == true) {
width = -129;
} else if (left == false) {
width = 129;
}
repaint();
if (System.currentTimeMillis() > curTime + 5000) {
fight = false;
file = "res/character.png";
if (left == true) {
width = -100;
} else if (left == false) {
width = 100;
}
repaint();
}
}
public void rand() {
if (x <= -130) {
x = 770;
} else if (x >= 770) {
x = -130;
}
}
}