[英]My java program stops working correctly unexpectedly
I am programming a basic java game, but I am having an issue.我正在编写一个基本的 Java 游戏,但遇到了问题。 Everytime that I try it out, if I wait just for 10 seconds, the program stops working correctly.
每次我尝试它时,如果我只等待 10 秒钟,程序就会停止正常工作。 I've made a class called
Drawable
, which has a paint
function.我创建了一个名为
Drawable
的类,它有一个paint
功能。 This paint
function paints a rectangle on a certain area(given on the constructor).这个
paint
函数在特定区域绘制一个矩形(在构造函数中给出)。 And I have a thread that iterates over all drawables in an arraylist(added randomly, with another thread), and just substracts 1 to their x
.我有一个线程遍历数组列表中的所有可绘制对象(随机添加,使用另一个线程),然后将 1 减去它们的
x
。 When it stops working correctly, the character can jump and does all animations, but the drawables stop moving.当它停止正常工作时,角色可以跳跃并执行所有动画,但可绘制对象停止移动。 At first I thought this might have given a
ConcurrentModificationException
error, but it didn't print it on the console.起初我认为这可能会导致
ConcurrentModificationException
错误,但它没有在控制台上打印出来。 So right now I don't really know what to do.所以现在我真的不知道该怎么办。 Here I add the
Drawables
:在这里,我添加了
Drawables
:
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
while (Game.isPlayingGame) {
try {
Thread.sleep((long) (Math.random()*2000));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
obstacles.add(new Drawable(
Constants.WIDTH,
(int) (Constants.HEIGHT / 2),
Constants.WIDTH - 100,
(int) (Constants.HEIGHT / 2) - 100,
Color.BLUE));
}
}
});
t2.start();
Here I move the Drawables
:我在这里移动
Drawables
:
Thread t = new Thread(new Runnable() {
@Override
public void run() {
while (Game.isPlayingGame) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (Drawable d : obstacles) {
d.x -= 1;
d.x2 -= 1;
if (d.x2 < 0) {
obstacles.remove(d);
}
}
}
}
});
t.start();
paint method:涂装方法:
@Override
public void paint(Graphics g) {
super.paint(g);
floor.paint(g);
Graphics2D g2d = (Graphics2D) g;
AffineTransform ant = g2d.getTransform();
g2d.rotate(Math.toRadians(rotation),
character.x - Constants.characterSize / 2,
character.y - Constants.characterSize / 2);
character.paint(g);
g2d.setTransform(ant);
for (Drawable d : obstacles) {
d.paint(g);
System.out.println(rotation_down);
if (!rotation_down) {
if (!character.onCollision(floor)) {
character.y += (int) gravityAccel; // gravity
character.y2 += (int) gravityAccel; // gravity
gravityAccel += 0.1;
} else {
Screen.canJump = true;
gravityAccel = 0;
}
}
}
repaint();
}
Thanks in advance.提前致谢。
The proper synchronization depends on the exact internals of your Drawable.正确的同步取决于 Drawable 的确切内部结构。 Also use CopyOnWriteArrayList.
还可以使用 CopyOnWriteArrayList。 For the Drawable class, decrement of x and x2 should be atomic and synchronized with at least paint() method:
对于 Drawable 类, x 和 x2 的递减应该是原子的,并且至少与paint()方法同步:
synchronized moveToLeft() {
x-=1;
x2-=1;
}
However, it does not make sense to have an object with x2 < 0 but this is a separate discussion.然而,拥有 x2 < 0 的对象是没有意义的,但这是一个单独的讨论。
You would also want to have你也想拥有
synchronized getX2() {
return x2;
}
and in your second thread do something like this:在你的第二个线程中做这样的事情:
if (d.getX2 == 0) {
obstacles.remove(d);
}
else {
d.moveToLeft();
}
The reason to do the check first is that if you do it other way around, you can and up in a situation when x2 is already -1, obstacles.remove(d)
has not been called yet, and d.paint()
is called.首先进行检查的原因是,如果您以相反的方式进行检查,则在 x2 已经为 -1、
obstacles.remove(d)
d.paint()
obstacles.remove(d)
尚未被调用且d.paint()
是叫。 This could cause issues unless your paint() method can handle negative coordinates.这可能会导致问题,除非您的paint() 方法可以处理负坐标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.