2017-08-02 6 views
-1

私は自分でこのことを理解できると確信していますが、私はそれがすでに高価で結合しているように感じています。ループ内の関数をアクティブにしてループを終了した後に続ける方法

私はセキュリティロボットと侵入者でシミュレータを作成していますが、3番目のif文で侵入者(幅11の長方形)が監視カメラ(円弧)に衝突したかどうかをチェックし、ifセキュリティロボットが侵入者を追跡するための私の機能を有効にしたい。

< - 重要な注意:checkShapeIntersection機能は、タイムラインイベント内でその実行ので常に - >

private boolean checkShapeIntersection(Shape block) { 
    //Name to check for intruder and surveillance collision 
    String Surveillance = "Arc"; 

    boolean collisionDetected = false; 
    for (Shape static_bloc : nodes) { 
     if (static_bloc != block) { 
      Shape intersect = Shape.intersect(block, static_bloc); 
      if (intersect.getBoundsInLocal().getWidth() != -1) { 
       collisionDetected = true; 
       //Checks of intruder collides with an arc (surveillance), the 11th width is only for intruder rectangles 
       if (Surveillance.equals(block.getClass().getSimpleName()) && static_bloc.getBoundsInLocal().getWidth() == 11) { 
        //Activate Chase function 
        testRobot.chaseIntruder(static_bloc); 
        detected = true; 
       } 
      } 
     } 
    } 

    if (collisionDetected) { 
     block.setFill(Color.BLUE); 
    } else { 
     block.setFill(Color.RED); 
    } 

    return detected; 
} 

そして、私のセキュリティロボットクラスの内部

public void chaseIntruder(Shape intruder) { 
    destinationsList.add(new Vector2D(intruder.getLayoutBounds().getMinX(),intruder.getLayoutBounds().getMinY())); 
    this.updatePosition(); 
} 
+0

ループの後に続行することは何を意味しますか?ループで新しいスレッドを作成することはありません。したがって、メソッドは一度呼び出され、処理が終了すると、メソッドを呼び出す行の後に評価が続行されます。 – Milkmaid

+0

現在の書面では、私のセキュリティロボットは侵入者に向かって移動するだけですが、最後のif文が満たされると、侵入者がアークを離れるとすぐにロボットは侵入者に向かって動きを止めますが、ロボットが侵入者彼がキャッチするまで。 – bigPoppa350

答えて

1

おそらく、使用したいですここではマルチスレッド、あなたは希望の機能を達成するためにRunnableまたはThreadを実装することができます。ここで

は、より多くの情報といくつかのリンクです:

In a simple to understand explanation, what is Runnable in Java?

https://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html

https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html

https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html

それがお役に立てば幸いです。

関連する問題