【发布时间】:2018-11-02 19:52:06
【问题描述】:
当我在 x 和 y 的开始位置以及 x 和 y 渲染的最终位置下方运行代码时。我希望在 tick 方法运行时渲染 x 和 y 的每个位置,并通过速度更改 x 和 y 值。基本上我希望能够控制物体移动时的速度。
public void render(Graphics g) {
g.drawImage(JamesTexture.james, x, y, width, height, null);
}
public void tick() {
if (run) {
if (!stop) {
int z = Maze.fx.size() - 1;
int i = 1;// speed
xx = getPathX(z);
yy = getPathY(z);
while (z > 0) {
if (xx > (getPathX(z - 1))) {
xx = xx - i;
x = xx;
if (xx < (getPathX(z - 1))) {
xx = (getPathX(z - 1));
}
} else if (xx < (getPathX(z - 1))) {
xx = xx + i;
x = xx;
if (xx > (getPathX(z - 1))) {
xx = (getPathX(z - 1));
}
} else if (yy > (getPathY(z - 1))) {
yy = yy - i;
y = yy;
if (yy < (getPathY(z - 1))) {
yy = (getPathY(z - 1));
}
} else if (yy < (getPathY(z - 1))) {
yy = yy + i;
y = yy;
if (yy < (getPathY(z - 1))) {
yy = (getPathY(z - 1));
}
} else {
z--;
stop = true; }
}
}
}
}
}
int getPathX(int z) {
List<Integer> fx = Maze.fx;
z = fx.get(z);
return z * 32;
}
int getPathY(int z) {
List<Integer> fy = Maze.fy;
z = fy.get(z);
return z * 32;
}
【问题讨论】:
-
您需要使用定时器,这样每次定时器触发时,您都会根据新位置重新绘制场景。
-
这个 GUI 是否使用 Swing、AWT、..crayons 来制作 GUI?