eXpect :
first, sorry for my grammar. i need to create Bomb object and paint black and 5 second later it will paint in red without freezing my game, i try to make Bomber Man game. Thanks
import java.awt.*;
public class Bomb {
int x,y;
int block = 60;
public Bomb(int x,int y) {
this.x = x;
this.y = y;
}
void draw(Graphics g) {
g.setColor(Color.BLACK);
g.fillOval(this.x * block, this.y * block, block, block);
if ( /*TIMER == 0*/) {
g.setColor(Color.RED);
g.fillOval(this.x * block, this.y * block, block, block);
}
}
}
VGR :
You need to do more than just change the color of your Bomb object. Your game needs to keep track of the Bomb’s state. Therefore, you need to create a Timer in a different class, and have that Timer’s action listener change an attribute of the Bomb which the Bomb class uses to determine how it paints:
public class Bomb {
boolean expired;
// ...
void draw(Graphics g) {
g.setColor(expired ? Color.RED : Color.BLACK);
g.fillOval(this.x * block, this.y * block, block, block);
}
}
Whatever class controls your game’s logic will need to create a Timer whenever it creates a Bomb:
Bomb bomb = new Bomb(x, y);
Timer timer = new Timer(10_000, e -> {
bomb.expired = true;
});
timer.setRepeats(false);
timer.start();
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=276889&siteId=1