我试图创建一个计时器,该计时器将在我创建的javafx游戏中单击矩形时延长其长度。单击黑色或红色矩形时,时间轴关键帧Duration.millis()
方法将延长10秒。由于某些原因,我无法延长计时器的长度以增加更多时间。当矩形被成功单击时,是否可以更改时间轴对象以增加更多时间?或者还有其他方法吗?谢谢
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.animation.KeyFrame;
import javafx.animation.PathTransition;
import javafx.animation.RotateTransition;
import javafx.animation.Timeline;
public class NinjaClicker extends Application {
// integer for storing score
int score = 0;
@Override
public void start(Stage primaryStage) {
// Bug objects
BlackBug blackBug = new BlackBug();
RedBug redBug = new RedBug();
BlueBug blueBug = new BlueBug();
GreenBug greenBug = new GreenBug();
// Pane object
Pane pane = new Pane();
pane.setStyle("-fx-background-color:#00000000;");
// HBox Object for containing the score label, score textfield, and close button
HBox hBox = new HBox(10);
hBox.setAlignment(Pos.CENTER);
Label scoreLabel = new Label("Score:");
TextField scoreTextField = new TextField();
Button close = new Button("Close");
close.setOnAction(e->{
primaryStage.close();
});
scoreTextField.setPrefColumnCount(20);
hBox.getChildren().addAll(scoreLabel, scoreTextField, close);
// Rectangle objects that represent each type of bug
Rectangle blackRectangle = new Rectangle(0,0,20,15);
blackRectangle.setFill(Color.BLACK);
blackRectangle.setStroke(Color.BLACK);
blackRectangle.setStrokeWidth(2);
Rectangle redRectangle = new Rectangle(0,0,30,25);
redRectangle.setFill(Color.RED);
redRectangle.setStroke(Color.BLACK);
redRectangle.setStrokeWidth(2);
Rectangle blueRectangle = new Rectangle(0,0,40,35);
blueRectangle.setFill(Color.BLUE);
blueRectangle.setStroke(Color.BLACK);
blueRectangle.setStrokeWidth(2);
Rectangle greenRectangle = new Rectangle(0, 0, 50, 45);
greenRectangle.setFill(Color.GREEN);
greenRectangle.setStroke(Color.BLACK);
greenRectangle.setStrokeWidth(2);
// Line objects
Line blackLine = new Line(0,0,500,500);
blackLine.setStroke(Color.ALICEBLUE);
Line redLine = new Line(250, 0, 250, 500);
redLine.setStroke(Color.ALICEBLUE);
Line blueLine = new Line(0,250,500,250);
blueLine.setStroke(Color.ALICEBLUE);
Line greenLine = new Line(500,0,0,500);
greenLine.setStroke(Color.ALICEBLUE);
// Add rectangles to pane object
pane.getChildren().add(redRectangle);
pane.getChildren().add(blackRectangle);
pane.getChildren().add(greenRectangle);
pane.getChildren().add(blueRectangle);
pane.getChildren().add(redLine);
pane.getChildren().add(blackLine);
pane.getChildren().add(greenLine);
pane.getChildren().add(blueLine);
// Pathtransitions
PathTransition blackPath = new PathTransition();
blackPath.setDuration(Duration.millis(5000));
blackPath.setPath(blackLine);
blackPath.setNode(blackRectangle);
blackPath.setCycleCount(Timeline.INDEFINITE);
blackPath.setAutoReverse(true);
blackPath.play();
PathTransition redPath = new PathTransition();
redPath.setDuration(Duration.millis(8000));
redPath.setPath(redLine);
redPath.setNode(redRectangle);
redPath.setCycleCount(Timeline.INDEFINITE);
redPath.setAutoReverse(true);
redPath.play();
PathTransition bluePath = new PathTransition();
bluePath.setDuration(Duration.millis(11000));
bluePath.setPath(blueLine);
bluePath.setNode(blueRectangle);
bluePath.setCycleCount(Timeline.INDEFINITE);
bluePath.setAutoReverse(true);
bluePath.play();
PathTransition greenPath = new PathTransition();
greenPath.setDuration(Duration.millis(14000));
greenPath.setPath(greenLine);
greenPath.setNode(greenRectangle);
greenPath.setCycleCount(Timeline.INDEFINITE);
greenPath.setAutoReverse(true);
greenPath.play();
// Rotate Transition
RotateTransition rotate = new RotateTransition();
rotate.setAxis(Rotate.Z_AXIS);
rotate.setByAngle(360);
rotate.setCycleCount(500);
rotate.setDuration(Duration.millis(10000));
rotate.setAutoReverse(true);
rotate.setNode(pane);
rotate.play();
// Borderpane that contains the pane and hBox objects
BorderPane borderPane = new BorderPane();
borderPane.setCenter(pane);
borderPane.setBottom(hBox);
// Place the borderPane in the scene and the scene inside the stage.
Scene scene = new Scene(borderPane, 500, 500);
primaryStage.setTitle("Ninja Clicker");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
// Timeline object
Timeline endTheGame = new Timeline(new KeyFrame(
Duration.millis(20000),
ae -> gameOver()));
endTheGame.play();
endTheGame.setOnFinished(e->{
primaryStage.close();
});
/* Event handling that removes rectangles, increases points, and increases timeline
if either a black or red rectangle is clicked */
blackRectangle.setOnMouseClicked(e ->{
pane.getChildren().remove(blackRectangle);
score = score + blackBug.getPoints();
endTheGame.stop();
scoreTextField.setText("Good job! Your score is " + score);
});
redRectangle.setOnMouseClicked(e ->{
pane.getChildren().remove(redRectangle);
score = score + redBug.getPoints();
scoreTextField.setText("Good job! Your score is " + score);
});
blueRectangle.setOnMouseClicked(e ->{
pane.getChildren().remove(blueRectangle);
score = score + blueBug.getPoints();
scoreTextField.setText("Good job! Your score is " + score);
});
greenRectangle.setOnMouseClicked(e ->{
pane.getChildren().remove(greenRectangle);
score = score + greenBug.getPoints();
scoreTextField.setText("Good job! Your score is " + score);
});
}
public static void main(String[] args) {
launch(args);
}
// Method to signal the end of the game
public void gameOver() {
// Stage for showing window
Stage stage = new Stage();
// BorderPane object for storing hBox and timerText.
BorderPane pane = new BorderPane();
HBox hBox = new HBox(10);
Button closeSystem = new Button("Close");
closeSystem.setOnAction(e->{
System.exit(0);
});
hBox.getChildren().add(closeSystem);
Text timerText = new Text("Times Up! Your score is " + score);
pane.setCenter(timerText);
pane.setBottom(hBox);
// Scene in the Stage
Scene scene = new Scene(pane, 200,200);
stage.setScene(scene);
stage.setTitle("Times Up!");
stage.show();
}
}
您可以创建一个倒计时变量并重组您的时间轴。
重组时间轴对象和倒计时变量
AtomicInteger atomicInteger = new AtomicInteger(20);
endTheGame = new Timeline(new KeyFrame(
Duration.seconds(1),
ae -> {
atomicInteger.decrementAndGet();
System.out.println(atomicInteger.get());
if(atomicInteger.get() == 0)
{
gameOver();
endTheGame.stop();
primaryStage.close();
}
}
));
endTheGame.setCycleCount(Timeline.INDEFINITE);
endTheGame.play();
单击矩形时如何增加时间的示例:
blackRectangle.setOnMouseClicked(e ->{
pane.getChildren().remove(blackRectangle);
// score = score + blackBug.getPoints();
//endTheGame.stop();
atomicInteger.set(atomicInteger.get() + 5);
scoreTextField.setText("Good job! Your score is " + score);
});
完整代码:
import java.util.concurrent.atomic.AtomicInteger;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.animation.KeyFrame;
import javafx.animation.PathTransition;
import javafx.animation.RotateTransition;
import javafx.animation.Timeline;
public class Main extends Application {
// integer for storing score
int score = 0;
Timeline endTheGame;
@Override
public void start(Stage primaryStage) {
// Bug objects
//BlackBug blackBug = new BlackBug();
//RedBug redBug = new RedBug();
//BlueBug blueBug = new BlueBug();
//GreenBug greenBug = new GreenBug();
// Pane object
Pane pane = new Pane();
pane.setStyle("-fx-background-color:#00000000;");
// HBox Object for containing the score label, score textfield, and close button
HBox hBox = new HBox(10);
hBox.setAlignment(Pos.CENTER);
Label scoreLabel = new Label("Score:");
TextField scoreTextField = new TextField();
Button close = new Button("Close");
close.setOnAction(e->{
primaryStage.close();
});
scoreTextField.setPrefColumnCount(20);
hBox.getChildren().addAll(scoreLabel, scoreTextField, close);
// Rectangle objects that represent each type of bug
Rectangle blackRectangle = new Rectangle(0,0,20,15);
blackRectangle.setFill(Color.BLACK);
blackRectangle.setStroke(Color.BLACK);
blackRectangle.setStrokeWidth(2);
Rectangle redRectangle = new Rectangle(0,0,30,25);
redRectangle.setFill(Color.RED);
redRectangle.setStroke(Color.BLACK);
redRectangle.setStrokeWidth(2);
Rectangle blueRectangle = new Rectangle(0,0,40,35);
blueRectangle.setFill(Color.BLUE);
blueRectangle.setStroke(Color.BLACK);
blueRectangle.setStrokeWidth(2);
Rectangle greenRectangle = new Rectangle(0, 0, 50, 45);
greenRectangle.setFill(Color.GREEN);
greenRectangle.setStroke(Color.BLACK);
greenRectangle.setStrokeWidth(2);
// Line objects
Line blackLine = new Line(0,0,500,500);
blackLine.setStroke(Color.ALICEBLUE);
Line redLine = new Line(250, 0, 250, 500);
redLine.setStroke(Color.ALICEBLUE);
Line blueLine = new Line(0,250,500,250);
blueLine.setStroke(Color.ALICEBLUE);
Line greenLine = new Line(500,0,0,500);
greenLine.setStroke(Color.ALICEBLUE);
// Add rectangles to pane object
pane.getChildren().add(redRectangle);
pane.getChildren().add(blackRectangle);
pane.getChildren().add(greenRectangle);
pane.getChildren().add(blueRectangle);
pane.getChildren().add(redLine);
pane.getChildren().add(blackLine);
pane.getChildren().add(greenLine);
pane.getChildren().add(blueLine);
// Pathtransitions
PathTransition blackPath = new PathTransition();
blackPath.setDuration(Duration.millis(5000));
blackPath.setPath(blackLine);
blackPath.setNode(blackRectangle);
blackPath.setCycleCount(Timeline.INDEFINITE);
blackPath.setAutoReverse(true);
blackPath.play();
PathTransition redPath = new PathTransition();
redPath.setDuration(Duration.millis(8000));
redPath.setPath(redLine);
redPath.setNode(redRectangle);
redPath.setCycleCount(Timeline.INDEFINITE);
redPath.setAutoReverse(true);
redPath.play();
PathTransition bluePath = new PathTransition();
bluePath.setDuration(Duration.millis(11000));
bluePath.setPath(blueLine);
bluePath.setNode(blueRectangle);
bluePath.setCycleCount(Timeline.INDEFINITE);
bluePath.setAutoReverse(true);
bluePath.play();
PathTransition greenPath = new PathTransition();
greenPath.setDuration(Duration.millis(14000));
greenPath.setPath(greenLine);
greenPath.setNode(greenRectangle);
greenPath.setCycleCount(Timeline.INDEFINITE);
greenPath.setAutoReverse(true);
greenPath.play();
// Rotate Transition
RotateTransition rotate = new RotateTransition();
rotate.setAxis(Rotate.Z_AXIS);
rotate.setByAngle(360);
rotate.setCycleCount(500);
rotate.setDuration(Duration.millis(10000));
rotate.setAutoReverse(true);
rotate.setNode(pane);
rotate.play();
// Borderpane that contains the pane and hBox objects
BorderPane borderPane = new BorderPane();
borderPane.setCenter(pane);
borderPane.setBottom(hBox);
// Place the borderPane in the scene and the scene inside the stage.
Scene scene = new Scene(borderPane, 500, 500);
primaryStage.setTitle("Ninja Clicker");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
// Timeline object
AtomicInteger atomicInteger = new AtomicInteger(20);
endTheGame = new Timeline(new KeyFrame(
Duration.seconds(1),
ae -> {
atomicInteger.decrementAndGet();
System.out.println(atomicInteger.get());
if(atomicInteger.get() == 0)
{
gameOver();
endTheGame.stop();
primaryStage.close();
}
}
));
endTheGame.setCycleCount(Timeline.INDEFINITE);
endTheGame.play();
/* Event handling that removes rectangles, increases points, and increases timeline
if either a black or red rectangle is clicked */
blackRectangle.setOnMouseClicked(e ->{
pane.getChildren().remove(blackRectangle);
// score = score + blackBug.getPoints();
//endTheGame.stop();
atomicInteger.set(atomicInteger.get() + 5);
scoreTextField.setText("Good job! Your score is " + score);
});
redRectangle.setOnMouseClicked(e ->{
pane.getChildren().remove(redRectangle);
//score = score + redBug.getPoints();
atomicInteger.set(atomicInteger.get() + 5);
scoreTextField.setText("Good job! Your score is " + score);
});
blueRectangle.setOnMouseClicked(e ->{
pane.getChildren().remove(blueRectangle);
//score = score + blueBug.getPoints();
scoreTextField.setText("Good job! Your score is " + score);
atomicInteger.set(atomicInteger.get() + 5);
});
greenRectangle.setOnMouseClicked(e ->{
pane.getChildren().remove(greenRectangle);
//score = score + greenBug.getPoints();
scoreTextField.setText("Good job! Your score is " + score);
atomicInteger.set(atomicInteger.get() + 5);
});
}
public static void main(String[] args) {
launch(args);
}
// Method to signal the end of the game
public void gameOver() {
// Stage for showing window
Stage stage = new Stage();
// BorderPane object for storing hBox and timerText.
BorderPane pane = new BorderPane();
HBox hBox = new HBox(10);
Button closeSystem = new Button("Close");
closeSystem.setOnAction(e->{
System.exit(0);
});
hBox.getChildren().add(closeSystem);
Text timerText = new Text("Times Up! Your score is " + score);
pane.setCenter(timerText);
pane.setBottom(hBox);
// Scene in the Stage
Scene scene = new Scene(pane, 200,200);
stage.setScene(scene);
stage.setTitle("Times Up!");
stage.show();
}
}