In meinem Programm, wenn meine zwei Rechtecke übereinstimmen, wird die Punktzahl um eins erhöht. Aber es funktioniert nur einmal. Ich verstehe, dass ich eine Schleife in mein Programm einfügen muss, ich habe nur einige Probleme, wie der Zustand sein sollte. Anstelle einer if-Anweisung habe ich eine while() -Schleife eingefügt und es hat immer noch nicht funktioniert. Bitte helfen Sie. Sie werden sehen, wo das Problem beginnt, ich werde einen Kommentar abgeben.Wann sollte der Zustand meiner Schleife sein?
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.control.*;
import javafx.event.Event;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.shape.*;
import javafx.scene.paint.Color;
import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.util.Duration;
import java.lang.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.FadeTransition;
import javafx.geometry.Bounds;
public class TransoFo extends Application{
public void start(Stage stage) {
Pane p = new Pane();
Button b = new Button("Play");
b.setStyle("-fx-background-radius: 3em;" +
"-fx-background-color: #66a3ff;" +
"-fx-min-width: 120;" +
"-fx-min-height: 40;" +
"-fx-max-width: 120;" +
"-fx-min-height: 40;" +
"-fx-cursor: hand;" +
"-fx-text-fill: white;");
b.setLayoutX(307);
b.setLayoutY(400);
Circle bi = new Circle();
Rectangle biRec = new Rectangle();
Circle circle = new Circle();
Rectangle rec = new Rectangle();
rec.setWidth(20);
rec.setHeight(30);
rec.setArcWidth(5);
rec.setArcHeight(5);
rec.setStyle("-fx-fill: #ff9933;" +
"-fix-stroke-width: 20;" +
"-fix-stroke: #ff4d4d;");
circle.setStyle("-fx-fill: #88ff4d;" +
"-fx-stroke-width: 12;" +
"-fx-stroke: #3399ff;");
circle.setCenterX(370);
circle.setCenterY(250);
circle.setRadius(50);
biRec.setWidth(30);
biRec.setHeight(20);
biRec.setArcWidth(5);
biRec.setArcHeight(5);
biRec.setStyle("-fx-fill: #ff9933;" +
"-fix-stroke-width: 20;" +
"-fix-stroke: #ff4d4d;");
bi.setStyle("-fx-fill: #88ff4d;" +
"-fx-stroke-width: 12;" +
"-fx-stroke: #3399ff;");
bi.setCenterX(370);
bi.setCenterY(250);
bi.setRadius(100);
p.getChildren().addAll(bi, biRec, circle, rec);
// transition for small circle and rectangle
PathTransition pt1 = new PathTransition();
pt1.setDuration(Duration.millis(1200));
pt1.setPath(bi);
pt1.setNode(biRec);
pt1.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
pt1.setCycleCount(Timeline.INDEFINITE);
pt1.setAutoReverse(false);
pt1.play();
// next path transition
PathTransition pt3 = new PathTransition();
pt3.setDuration(Duration.millis(800));
pt3.setPath(circle);
pt3.setNode(rec);
pt3.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
pt3.setCycleCount(Timeline.INDEFINITE);
pt3.setAutoReverse(false);
pt3.play();
b.setOnAction((ActionEvent event) -> {
bi.setVisible(false);
biRec.setVisible(false);
circle.setVisible(false);
rec.setVisible(false);
b.setVisible(false);
Circle big = new Circle();
Rectangle bigRec = new Rectangle();
Circle circ = new Circle();
Rectangle r = new Rectangle();
r.setWidth(20);
r.setHeight(30);
r.setArcWidth(5);
r.setArcHeight(5);
r.setStyle("-fx-fill: #ff9933;" +
"-fix-stroke-width: 20;" +
"-fix-stroke: #ff4d4d;");
circ.setStyle("-fx-fill: #88ff4d;" +
"-fx-stroke-width: 12;" +
"-fx-stroke: #3399ff;");
circ.setCenterX(370);
circ.setCenterY(300);
circ.setRadius(50);
bigRec.setWidth(30);
bigRec.setHeight(20);
bigRec.setArcWidth(5);
bigRec.setArcHeight(5);
bigRec.setStyle("-fx-fill: #ff9933;" +
"-fix-stroke-width: 20;" +
"-fix-stroke: #ff4d4d;");
big.setStyle("-fx-fill: #88ff4d;" +
"-fx-stroke-width: 12;" +
"-fx-stroke: #3399ff;");
big.setCenterX(370);
big.setCenterY(300);
big.setRadius(100);
PathTransition pt2 = new PathTransition();
pt2.setDuration(Duration.millis(1200));
pt2.setPath(big);
pt2.setNode(bigRec);
pt2.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
pt2.setCycleCount(Timeline.INDEFINITE);
pt2.setAutoReverse(true);
pt2.play();
PathTransition pt = new PathTransition();
pt.setDuration(Duration.millis(800));
pt.setPath(circ);
pt.setNode(r);
pt.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
pt.setCycleCount(Timeline.INDEFINITE);
pt.setAutoReverse(false);
pt.play();
Button b2 = new Button(" | | ");
b2.setStyle("-fx-background-radius: 3em;" +
"-fx-background-color: #66a3ff;" +
"-fx-min-width: 70;" +
"-fx-min-height: 40;" +
"-fx-max-width: 700;" +
"-fx-min-height: 40;" +
"-fx-cursor: cursor;" +
"-fx-text-fill: white;");
b2.setLayoutX(670);
b2.setLayoutY(10);
b2.setOnAction((ActionEvent event1) -> {
pt2.stop();
pt.stop();
});
Button b3 = new Button(" ▶ ");
b3.setStyle("-fx-background-radius: 3em;" +
"-fx-background-color: #66a3ff;" +
"-fx-min-width: 70;" +
"-fx-min-height: 40;" +
"-fx-max-width: 700;" +
"-fx-min-height: 40;" +
"-fx-cursor: hand;" +
"-fx-text-fill: white;");
b3.setLayoutX(590);
b3.setLayoutY(10);
b3.setOnAction((ActionEvent event2) -> {
pt.play();
pt2.play();
});
Button match = new Button(" Match ");
match.setStyle("-fx-background-radius: 3em;" +
"-fx-background-color: #66a3ff;" +
"-fx-min-width: 120;" +
"-fx-min-height: 40;" +
"-fx-max-width: 120;" +
"-fx-min-height: 40;" +
"-fx-cursor: hand;" +
"-fx-text-fill: white;");
match.setLayoutX(310);
match.setLayoutY(450);
match.setOnAction((ActionEvent evt) -> {
Bounds bBounds = bigRec.getBoundsInParent();
Bounds bounds = r.getBoundsInParent();
// problem starts here
int total = 0;
if (bounds.getMinY() <= bBounds.getMaxY() && bounds.getMaxY() >= bBounds.getMinY()) {
total += 1;
String str = Integer.toString(total);
Label score = new Label(str);
score.setFont(new Font("Courier New bold", 20));
score.setLayoutX(100);
score.setLayoutY(200);
p.getChildren().add(score);
pt.stop();
pt2.stop();
try {
Thread.sleep(1000);
pt.play();
pt2.play();
}
catch (Exception e) {}
}
// end the event
});
p.getChildren().addAll(big, bigRec, circ, r, b2, b3, match);
FadeTransition ft = new FadeTransition(Duration.millis(1500), big);
ft.setFromValue(0.1);
ft.setToValue(1);
ft.play();
//transition for big rec
FadeTransition ft2 = new FadeTransition(Duration.millis(1500), bigRec);
ft2.setFromValue(0.1);
ft2.setToValue(1);
ft2.play();
//transition for circ
FadeTransition ft3 = new FadeTransition(Duration.millis(1500), circ);
ft3.setFromValue(0.1);
ft3.setToValue(1);
ft3.play();
//transition for r
FadeTransition ft4 = new FadeTransition(Duration.millis(1500), r);
ft4.setFromValue(0.1);
ft4.setToValue(1);
ft4.play();
//transition for match
FadeTransition ft5 = new FadeTransition(Duration.millis(1500), match);
ft5.setFromValue(0.1);
ft5.setToValue(1);
ft5.play();
//transition for pause button
FadeTransition ft6 = new FadeTransition(Duration.millis(3000), b2);
ft6.setFromValue(0.1);
ft6.setToValue(1);
ft6.play();
//transtion for play button
FadeTransition ft7 = new FadeTransition(Duration.millis(3000), b3);
ft7.setFromValue(0.1);
ft7.setToValue(1);
ft7.play();
});
p.getChildren().add(b);
p.setStyle("-fx-background-color: #88ff4d;");
Scene s = new Scene(p, 750, 650);
stage.setResizable(false);
stage.setScene(s);
stage.show();
}
// launch Application
public static void main(String[] args) {
Application.launch(args);
}
}
Um bessere Antworten zu erhalten, versuchen Sie eine [mcve] zu erstellen - insbesondere gibt es eine Menge Code in Ihrer Frage, die völlig unabhängig von Ihrem Problem ist. – assylias
Ihr Gesamtzähler wird jedes Mal auf "0" zurückgesetzt, wenn Sie auf die Schaltfläche "Übereinstimmung" klicken. Ich schätze, das ist der Fehler. –