I’ve been having troubles with using an “if” statement in a timer. I have the timer set to run an if loop every second or so, but the action within the if statement executes even if the “if” conditions were not met. Am I doing something wrong here, or is this impossible?
Code:
//GLOBAL TIMERS
//car edge detection
int initialDelay = 0; // start after 0 seconds
int period = 50; // repeat every 5 seconds
final Timer carAI = new Timer();
TimerTask task = new TimerTask() {
public void run() {
if (redcar.getX() == -50); {
redcar.setIcon(new ImageIcon(gui.class.getResource("/main/redcar.png")));
redcar.setLocation(redcar.getX() + 5, redcar.getY());
}
}
};
carAI.scheduleAtFixedRate(task, initialDelay, period);
If @Oli’s hint isnt enough, let me rewrite it as the compiler sees your code.
Remove that semicolon to fix your issue.