JavaFX - 圆形和矩形之间的交互
JavaFX - Interaction between circle and rectangle
我想知道如何在矩形折叠成圆形时进行交互。就像圆形和矩形折叠时发生的某种动作。还有一个问题是圆可能会超出框架,我不知道如何限制它的移动。我什至不确定是否可以做我想做的事这个 way.This 应该是一个我需要躲避所有矩形的游戏,这是我能做的最好的。提前谢谢 :)
public class Java2 extends Application {
public static final int KRUG_WIDTH = 10;
public static final int PANEL_WIDTH = 600;
public static final int PANEL_HEIGHT = 600;
private int mX = (PANEL_WIDTH - KRUG_WIDTH) / 2;
private int mY = (PANEL_HEIGHT - KRUG_WIDTH) / 2;
Random ran = new Random();
@Override
public void start(Stage primaryStage) {
Rectangle rekt = new Rectangle(20, 20);
Rectangle rekt1 = new Rectangle(20, 20);
Circle r1 = new Circle(mX,mY,KRUG_WIDTH);
Pane root = new Pane(); //PANE
r1.setFill(Color.WHITE);
r1.setStroke(Color.BLACK);
root.getChildren().add(r1);
root.getChildren().add(rekt);
root.getChildren().add(rekt1);
Scene scene = new Scene(root, PANEL_WIDTH, PANEL_HEIGHT);
PathTransition pathTransition = new PathTransition();
Path path = new Path();
//REKT-PATH
pathTransition.setDuration(javafx.util.Duration.millis(600));
pathTransition.setPath(path);
pathTransition.setNode(rekt);
pathTransition.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
pathTransition.setCycleCount(2);
pathTransition.setAutoReverse(true);
pathTransition.setOnFinished(e -> {
pathTransition.setPath(createPath());
pathTransition.play();
});
pathTransition.play();
PathTransition pathTransition1 = new PathTransition();
Path path1 = new Path();
//REKT1-PATH
pathTransition1.setDuration(javafx.util.Duration.millis(550));
pathTransition1.setPath(path1);
pathTransition1.setNode(rekt1);
pathTransition1.setOrientation(
PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
pathTransition1.setCycleCount(Timeline.INDEFINITE);
pathTransition1.setAutoReverse(true);
pathTransition1.setOnFinished(e -> {
pathTransition1.setPath(createPath());
pathTransition1.play();
});
pathTransition1.play();
r1.setOnKeyPressed(e -> {
switch (e.getCode()) {
case DOWN: r1.setCenterY(r1.getCenterY()+ 10);
break;
case UP: r1.setCenterY(r1.getCenterY()- 10);
break;
case LEFT: r1.setCenterX(r1.getCenterX() - 10);
break;
case RIGHT: r1.setCenterX(r1.getCenterX() + 10);
break;
case SPACE:
break;
default:
}
});
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
r1.requestFocus();
}
private Path createPath() {
int loc2 = ran.nextInt(600 - 300 + 1) + 300;
int loc = ran.nextInt(600 - 20 + 1) + 20;
Path path = new Path();
path.getElements().add(new MoveTo(20, 20));
path.getElements().add(new LineTo(loc, loc2));
return path;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
我不会采用你的方法。通常你有一个计时器作为游戏循环,所有的魔法都在其中发生,我。 e.在 UI 中移动精灵、检查碰撞、更新精灵。在 JavaFX 中,这将是 AnimationTimer.
在您的代码中您遗漏了几件重要的事情,其中包括键盘或鼠标输入。或者你如何检查碰撞。有电子。 G。 intersects method for the nodes, but that checks only the rectangular bounds of a node. You however have a circle, which makes matters more complex. However, if performance isn't an issue for your game, you could use the Shape's intersect method 从矩形和圆形创建新形状,并根据结果决定是否发生碰撞。
前段时间我创建了关于 的示例代码并检查它们是否与其他代码发生碰撞。
关于在该代码中弹离场景边界,您只需检查场景边界并将移动增量更改为弹跳值,即。 e.如果精灵从左向右移动(dx 为正)并且位于右侧,则将 delta dx 设置为负值,使其向相反的方向移动。
您还可以查看 simple Pong game,它使用了该引擎的略微修改版本。
与您的代码相交的形状示例:
Shape shape = Shape.intersect(rekt, r1);
boolean intersects = shape.getBoundsInLocal().getWidth() != -1;
if( intersects) {
System.out.println( "Collision");
}
仅此一项就提出了您将把支票放在哪里的问题。通常你必须在任何东西移动时执行它,我。 e.在这样的两个路径转换中
pathTransition.currentTimeProperty().addListener( e -> {
...
});
并在圈内过渡。那将是太多的 2 个支票。
我想知道如何在矩形折叠成圆形时进行交互。就像圆形和矩形折叠时发生的某种动作。还有一个问题是圆可能会超出框架,我不知道如何限制它的移动。我什至不确定是否可以做我想做的事这个 way.This 应该是一个我需要躲避所有矩形的游戏,这是我能做的最好的。提前谢谢 :)
public class Java2 extends Application {
public static final int KRUG_WIDTH = 10;
public static final int PANEL_WIDTH = 600;
public static final int PANEL_HEIGHT = 600;
private int mX = (PANEL_WIDTH - KRUG_WIDTH) / 2;
private int mY = (PANEL_HEIGHT - KRUG_WIDTH) / 2;
Random ran = new Random();
@Override
public void start(Stage primaryStage) {
Rectangle rekt = new Rectangle(20, 20);
Rectangle rekt1 = new Rectangle(20, 20);
Circle r1 = new Circle(mX,mY,KRUG_WIDTH);
Pane root = new Pane(); //PANE
r1.setFill(Color.WHITE);
r1.setStroke(Color.BLACK);
root.getChildren().add(r1);
root.getChildren().add(rekt);
root.getChildren().add(rekt1);
Scene scene = new Scene(root, PANEL_WIDTH, PANEL_HEIGHT);
PathTransition pathTransition = new PathTransition();
Path path = new Path();
//REKT-PATH
pathTransition.setDuration(javafx.util.Duration.millis(600));
pathTransition.setPath(path);
pathTransition.setNode(rekt);
pathTransition.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
pathTransition.setCycleCount(2);
pathTransition.setAutoReverse(true);
pathTransition.setOnFinished(e -> {
pathTransition.setPath(createPath());
pathTransition.play();
});
pathTransition.play();
PathTransition pathTransition1 = new PathTransition();
Path path1 = new Path();
//REKT1-PATH
pathTransition1.setDuration(javafx.util.Duration.millis(550));
pathTransition1.setPath(path1);
pathTransition1.setNode(rekt1);
pathTransition1.setOrientation(
PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
pathTransition1.setCycleCount(Timeline.INDEFINITE);
pathTransition1.setAutoReverse(true);
pathTransition1.setOnFinished(e -> {
pathTransition1.setPath(createPath());
pathTransition1.play();
});
pathTransition1.play();
r1.setOnKeyPressed(e -> {
switch (e.getCode()) {
case DOWN: r1.setCenterY(r1.getCenterY()+ 10);
break;
case UP: r1.setCenterY(r1.getCenterY()- 10);
break;
case LEFT: r1.setCenterX(r1.getCenterX() - 10);
break;
case RIGHT: r1.setCenterX(r1.getCenterX() + 10);
break;
case SPACE:
break;
default:
}
});
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
r1.requestFocus();
}
private Path createPath() {
int loc2 = ran.nextInt(600 - 300 + 1) + 300;
int loc = ran.nextInt(600 - 20 + 1) + 20;
Path path = new Path();
path.getElements().add(new MoveTo(20, 20));
path.getElements().add(new LineTo(loc, loc2));
return path;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
我不会采用你的方法。通常你有一个计时器作为游戏循环,所有的魔法都在其中发生,我。 e.在 UI 中移动精灵、检查碰撞、更新精灵。在 JavaFX 中,这将是 AnimationTimer.
在您的代码中您遗漏了几件重要的事情,其中包括键盘或鼠标输入。或者你如何检查碰撞。有电子。 G。 intersects method for the nodes, but that checks only the rectangular bounds of a node. You however have a circle, which makes matters more complex. However, if performance isn't an issue for your game, you could use the Shape's intersect method 从矩形和圆形创建新形状,并根据结果决定是否发生碰撞。
前段时间我创建了关于
关于在该代码中弹离场景边界,您只需检查场景边界并将移动增量更改为弹跳值,即。 e.如果精灵从左向右移动(dx 为正)并且位于右侧,则将 delta dx 设置为负值,使其向相反的方向移动。
您还可以查看 simple Pong game,它使用了该引擎的略微修改版本。
与您的代码相交的形状示例:
Shape shape = Shape.intersect(rekt, r1);
boolean intersects = shape.getBoundsInLocal().getWidth() != -1;
if( intersects) {
System.out.println( "Collision");
}
仅此一项就提出了您将把支票放在哪里的问题。通常你必须在任何东西移动时执行它,我。 e.在这样的两个路径转换中
pathTransition.currentTimeProperty().addListener( e -> {
...
});
并在圈内过渡。那将是太多的 2 个支票。