我正在尝试制作一个程序,使您产生键入的球的数量。
一半的球在左侧(团队蓝色)生成,另一半在右侧(团队橙色)生成。每个球都必须到达另一侧而不与任何球发生碰撞。为了做到这一点,我首先需要使代码知道何时/是否会与另一个球发生碰撞。
在我的Type: CNAME
Name: www
value: example.io.s3-website.eu-west-2.amazonaws.com
TTL: 1 hour
类中,碰撞检测可以正常工作,但是我不知道如何解决该问题。我没有尝试从GUI类中调用它,但是我也不知道该怎么做。
我一直在寻找此链接以寻求帮助:Checking Collision of Shapes with JavaFX
但这没有帮助。请提出建议。
Ball.java
Ball
GUI.java
package fx;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Shape;
import javafx.util.Duration;
import javafx.scene.shape.*;
public class Ball {
public static int id;
public Circle circle;
public int team;
public Ball(int x, int y, int _id, int _team) {
id = _id;
Circle ball = new Circle(10, Color.BLACK);
ball.relocate(x, y);
circle = ball;
team = _team;
}
public void moveBall() {
System.out.println(this.team);
//team blue
if (this.team == 0) {
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(3),
new KeyValue(this.circle.layoutXProperty(),
980-((Circle)this.circle).getRadius())));
timeline.setAutoReverse(true);
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
}//team orange
else if (this.team == 1) {
//System.out.println(this.circle.layoutXProperty());
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(3),
new KeyValue(this.circle.layoutXProperty(),
35-((Circle)this.circle).getRadius())));
timeline.setAutoReverse(true);
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
}
}
public Circle getCircle() {
return this.circle;
}
//collision detection
public void collisionDetection(Shape balls) {
boolean collisionDetected = false;
for(Shape static_bloc : circle) {
if(static_bloc != balls) {
Shape intersect = Shape.intersect(balls, static_bloc);
if (intersect.getBoundsInLocal().getWidth() != -1){
collisionDetected = true;
}
}
}
if(collisionDetected) {
}else {
}
}
}