2016-05-08 45 views
0

私は、しばらくして消える弾丸(小さな円)を発射する宇宙船(大きな円)を含むプログラムを持っています。 コードの特定の部分を実行するたびに、エラーが生成されます(ただし、引き続き実行されます)。エラー:スレッド「JavaFXアプリケーションスレッド」の例外

だから私は問題は次の行に

private ArrayList<Bullet> bullets = new ArrayList<Bullet>(); 
//[...] 
for (Bullet bullet : bullets) { 
    if (bullet.getLoops() > 50) { 

    bullets.remove(bullet); // specially in this line, 
          // because if I delete this, the error doesn't show up! 
          // I think it has something to do with the ArrayList. 

    } else { 
    bullet.next(); 
    gc.strokeOval(bullet.getX() - 3, bullet.getY() - 3, 6, 6); 
    } 
} 

であり、これは私が取得エラーであることを確信している:

Exception in thread "JavaFX Application Thread" java.util.ConcurrentModificationException 
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901) 
    at java.util.ArrayList$Itr.next(ArrayList.java:851) 
    at sample.Main$4.handle(Main.java:131) 
    at javafx.animation.AnimationTimer$AnimationTimerReceiver.lambda$handle$484(AnimationTimer.java:57) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at javafx.animation.AnimationTimer$AnimationTimerReceiver.handle(AnimationTimer.java:56) 
    at com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(AbstractMasterTimer.java:357) 
    at com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(AbstractMasterTimer.java:267) 
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:506) 
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:490) 
    at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$404(QuantumToolkit.java:319) 
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) 
    at java.lang.Thread.run(Thread.java:745) 

私は時に必要なその他の情報を提供するために喜んでされます要求。

乾杯。 (私の英語のため申し訳ありませんが、私はドイツから来たんだが、私は数年以来、学校で英語を持っています。)

PS:

package sample; 

import javafx.animation.AnimationTimer; 
import javafx.application.Application; 
import javafx.event.EventHandler; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.canvas.Canvas; 
import javafx.scene.canvas.GraphicsContext; 
import javafx.scene.input.KeyEvent; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.paint.Color; 
import javafx.scene.text.Font; 
import javafx.scene.text.FontWeight; 
import javafx.stage.Stage; 

import java.util.ArrayList; 

public class Main extends Application { 

    private Player p = new Player(100, 100, 0); 
    private ArrayList<Bullet> bullets = new ArrayList<Bullet>(); 

    private static boolean left; 
    private static boolean right; 
    private static boolean up; 
    private static boolean down; 
    private static boolean shoot; 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     FXMLLoader.load(getClass().getResource("sample.fxml")); 
     primaryStage.setTitle("Hello World"); 
     Group root0 = new Group(); 
     Scene theScene = new Scene(root0, 500, 500); 
     primaryStage.setScene(theScene); 
     Canvas canvas = new Canvas(500, 500); 
     root0.getChildren().add(canvas); 


     theScene.setOnMouseMoved(
       new EventHandler<MouseEvent>() { 
        public void handle(MouseEvent e) { 
         Vector a = new Vector(p.getX(), -p.getY()); 
         Vector b = new Vector(e.getX(), -e.getY()); 
         Vector c = new Vector(p.getX(), -p.getY() + 1); 
         p.setRotation(Vector.getAngle(a, b, c)); 
        } 
       } 
     ); 
     theScene.setOnKeyPressed(
       new EventHandler<KeyEvent>() { 
        public void handle(KeyEvent e) { 
         String code = e.getCode().toString(); 
         if (code.equals("A")) { 
          left = true; 
         } 
         if (code.equals("D")) { 
          right = true; 
         } 
         if (code.equals("W")) { 
          up = true; 
         } 
         if (code.equals("S")) { 
          down = true; 
         } 
         if (code.equals("SPACE")) { 
          if (!PlayerShot()) shoot = true; 
         } 
        } 
       } 
     ); 
     theScene.setOnKeyReleased(
       new EventHandler<KeyEvent>() { 
        public void handle(KeyEvent e) { 
         String code = e.getCode().toString(); 
         if (code.equals("A")) { 
          left = false; 
         } 
         if (code.equals("D")) { 
          right = false; 
         } 
         if (code.equals("W")) { 
          up = false; 
         } 
         if (code.equals("S")) { 
          down = false; 
         } 
         if (code.equals("SPACE")) { 
          shoot = false; 
         } 
        } 
       } 
     ); 
     GraphicsContext gc = canvas.getGraphicsContext2D(); 

     Font theFont = Font.font("Helvetica", FontWeight.BOLD, 24); 
     gc.setFont(theFont); 
     gc.setStroke(Color.BLACK); 
     gc.setLineWidth(1); 

     new AnimationTimer() { 
      public void handle(long currentNanoTime) { 
       // Clear the canvas 
       gc.setFill(new Color(0.85, 0.85, 1.0, 1.0)); 
       gc.fillRect(0, 0, 500, 500); 

       int x = p.getX(); 
       int y = p.getY(); 
       if (left && x >= 16) { 
        p.setX(x - 3); 
       } 
       if (right && x <= 500 - 16) { 
        p.setX(x + 3); 
       } 
       if (up && y >= 16) { 
        p.setY(y - 3); 
       } 
       if (down && y <= 500 - 16) { 
        p.setY(y + 3); 
       } 
       if (shoot) { 
        bullets.add(new Bullet(p.getX(), p.getY(), p.getRotationInVelocity(), p)); 
        shoot = false; 
       } 
       gc.setStroke(Color.BLUE); 
       gc.strokeLine(p.getX(), p.getY(), p.getX() + Math.sin(p.getRotation()) * 16, p.getY() - Math.cos(p.getRotation()) * 16); 
       gc.strokeOval(p.getX() - 16, p.getY() - 16, 32, 32); 
       gc.setStroke(Color.RED); 
       for (Bullet bullet : bullets) { 
        if (bullet.getLoops() > 50) { 
         bullets.remove(bullet); 
        } else { 
         bullet.next(); 
         gc.strokeOval(bullet.getX() - 3, bullet.getY() - 3, 6, 6); 
        } 
       } 
      } 
     }.start(); 

     primaryStage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 

    // Hat der Spieler gerade eben schon geschossen? 
    public boolean PlayerShot() { 
     for (Bullet bullet : bullets) { 
      if(bullet.getShooter() == p)return true; 
     } 
     return false; 
    } 
} 

その他:誰もmainclassの完全なコードを必要とする場合クラスはいくつかのintおよびdouble-varsを処理します

+0

public void next(){ x + = velocity.getY()* 5; y + = -velocity.getX()* 5; ループ++; } //これは前方への移動を処理するコードであり、削除すると "bullets.remove(bullet);"それはそれよりも上に現れません。しかし、それは私が欲しい解決策ではありません!とにかく:)ありがとう! – doej1367

+0

! – doej1367

答えて

1

これはできません。あなたはConcurrentModificationExceptionを取得しています。

ConcurrentModificationExceptionとは?公式Javadocから

This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.

基本的に、あなたはあなたのリストを反復処理しています。ある時点で要素を削除すると、予期しない動作が発生する可能性があります。これを避けるために、この例外がスローされます。

どうすれば修正できますか?

ArrayListを循環させる代わりにIteratorを単に使用してください。 Iteratorは、方法add(T element)およびremove(T element)を使用します。

+0

あなたはこれが好きですか? イテレータ bulletsI = bullets.iterator(); while(bulletsI.hasNext()){ if(bulletsI.next()。getLoops()> 50){ bullets.remove(bulletsI.next()); } else { bulletsI.next()。next(); gc.strokeOval(bulletsI.next()。getX() - 3、bulletsI.next()。getY() - 3,6,6); } } それだけでももっと多くのエラーを返します。 – doej1367

+1

感謝! それは私が正しい軌道にいることをもう少し知ってくれました – doej1367

+0

いいえあなたはnext()too may timesを呼び出しています。繰り返しごとに1回呼び出す必要があります。 Next()はイテレータをリストの次の要素に移動します。彼が少なくとも1つの要素を持っていることを確認した後、繰り返しごとに5回呼び出すと、なぜエラーが増えているのか分かります。しばらくしてからこれをやってください:Bullet currentElement = bulletsI.next();次に他の場所でnext()を呼び出さずにこのオブジェクトにあなたのものを行います。 – Aurasphere

関連する問題