[英]Java: Exception in thread “AWT-EventQueue-0” java.util.ConcurrentModificationException
i am currently trying to code my own little 2D Game. 我目前正在尝试编写自己的小型2D游戏。 It's a Helicopter dodging rockets.
这是一架躲避火箭的直升机。 these rockets flighing from right to the left.
这些火箭从右向左飞舞。 If they are outside of the Panel they should be deleted.
如果它们不在面板中,则应将其删除。
this is how it looks like: 它是这样的:
private void doLogic() {
Vector<Sprite> trash = new Vector<Sprite>();
for (Movable mov : actors) {
mov.doLogic(delta);
Sprite check = (Sprite) mov;
if (check.remove) {
trash.add(check);
}
}
if (trash.size() > 0) {
for (Sprite s : trash) {
actors.remove(s);
}
}
for (int i = 0; i < actors.size(); i++) {
for (int n = i + 1; n < actors.size(); n++) {
Sprite s1 = actors.elementAt(i);
Sprite s2 = actors.elementAt(n);
s1.collidedWith(s2);
}
}
}
and my print: 和我的印刷品:
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (!isStarted()) {
return;
}
if (actors != null) {
for (Drawable draw : actors) {
draw.drawObjects(g);
}
}
}
that's the Error I get everytime a rocket leaves the Panelarea: 这是我每次火箭离开Panelarea时都会遇到的错误:
Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
at java.util.Vector$Itr``.checkForComodification(Vector.java:1156)
at java.util.Vector$Itr.next(Vector.java:1133)
at GamePanel.paintComponent(GamePanel.java:339)
at javax.swing.JComponent.paint(JComponent.java:1045)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5210)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
at javax.swing.RepaintManager.paint(RepaintManager.java:1249)
at javax.swing.JComponent._paintImmediately(JComponent.java:5158)
at javax.swing.JComponent.paintImmediately(JComponent.java:4969)
at javax.swing.RepaintManager$3.run(RepaintManager.java:808)
at javax.swing.RepaintManager$3.run(RepaintManager.java:796)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:769)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:718)
at javax.swing.RepaintManager.access$1100(RepaintManager.java:62)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1677)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
I hope you can help me :) 我希望你能帮帮我 :)
You can't remove()
from the collection while iterating it with the foreach-loop. 使用foreach循环进行迭代时,无法从集合中
remove()
。 You can do it by using a regular Iterator
though, with the Iterator's remove() method. 不过,您可以使用常规的
Iterator
,并使用Iterator的remove()方法来完成此操作。
In the paintComponent() you can use Collections.public static <T> List<T> unmodifiableList(List<? extends T> list)
. 在paintComponent()中,可以使用
Collections.public static <T> List<T> unmodifiableList(List<? extends T> list)
。 Just pass the actors list and get a copy of the list. 只需通过演员名单并获得名单的副本即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.