我有一个Java应用程序,您可以按下按钮并启动导弹(由红色5 x 5像素点表示)。点从A点移动到B点,这是好的。 我想知道的是如何在雷达上使点发出哔哔声。 这就是我到目前为止所做的:
public class Explosion {
public int x, y, ex, ey, sx, sy;
public String Country, target;
private Image img;
public boolean remove;
public int time = 0;
public int timer = 40;
public Explosion(String Country, String target) {
this.Country = Country;
this.target = target;
GetCoordsLaunch();
GetCoordsTarget();
}
public void GetCoordsTarget() {
if (target == "Ocean") {
ex = play.mh.x + 4 + 140;
ey = play.mh.y + 245 + 16;
}
}
public void GetCoordsLaunch() {
if (Country == "USA") {
x = play.mh.x + 4 + 86;
y = play.mh.y + 245 + 42;
}
}
public void tick() {
if (time >= timer) {
if (x > ex) {
x--;
}
if (x < ex) {
x++;
}
if (y > ey) {
y--;
}
if (y < ey) {
y++;
}
time = 0;
} else {
time++;
}
if (x == ex && y == ey) {
remove = true;
}
}
public void render(Graphics g) {
g.setColor(Color.red);
g.fillRect(x, y, 5, 5);
}
}
答案 0 :(得分:0)
你可以让它只绘制time
的某些部分。因此,当时间在你绘制的某个范围内时,不是你没有。因此它会打开和关闭。
尝试仅更改渲染以依赖于时间变量,仅在时间范围的前半部分执行任何绘制。我还假设timer
表示每次移动的刻度数
public void render(Graphics g) {
if(time < timer/2){
g.setColor(Color.red);
g.fillRect(x, y, 5, 5);
}
}
因此,如果你持续tick()
,它会在移动时开启和关闭。