这是我的第一个问题,我是JavaFX的新手。我编写了一个小游戏,但我不知道如何使重新启动按钮正常工作。这是缩短的代码:
public class Main extends Application {
private Pane root = new Pane();
private static Button btnNewGame = new Button( "Neues Spiel?" );
private static Sprite player = new Sprite( getRandomNumberInts( 590, 1230 ), getRandomNumberInts( 50, 670 ), 40, 40, /*"player",*/ Color.BLUE );
...
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle( long now ) {
update();
}
};
private Parent createContent() {
root.setPrefSize( 1280, 720 );
timer.start();
nextLevel();
return root;
}
private void nextLevel() {
root.getChildren().add( player );
...
}
private void update() {
snake.moveToTarget();
if ( snake.getBoundsInParent().intersects( player.getBoundsInParent() ) ) {
player.dead = true;
player.win = false;
}
if( player.dead ) {
root.getChildren().add( btnNewGame );
root.getChildren().clear();
timer.stop();
}
if( player.win ) {
root.getChildren().add( btnNewGame );
root.getChildren().clear();
timer.stop();
}
root.getChildren().removeIf( n -> {
Sprite s = ( Sprite ) n;
return s.dead;
});
}
void cleanup() {
timer.stop();
root.getChildren().clear();
}
@SuppressWarnings("incomplete-switch")
public void startGame( Stage stage ) {
Scene scene = new Scene( createContent() );
scene.setOnKeyPressed( e -> {
switch ( e.getCode() ) {
case W:
});
btnNewGame.setOnAction( e -> {
restart( stage );
});
stage.setScene( scene );
stage.setTitle( "ZZZZZnake - Von Christoph Gabauer" );
stage.getIcons().add( new Image( Main.class.getResourceAsStream( "icon.png" ) ) );
stage.setResizable( false );
stage.show();
}
void restart( Stage stage ) {
cleanup();
start( stage );
}
@Override
public void start( Stage primaryStage ) {
startGame( primaryStage );
}
private static class Sprite extends Rectangle {
//final String type;
double pSpeed = 5;
Sprite( int x, int y, int w, int h, /*String type,*/ Color color ){
super( w, h, color );
//this.type = type;
setTranslateX( x );
setTranslateY( y );
里面有一些德国小东西,但是不要让那分散您对问题的注意力。我认为Exception与启动或升级方法有关。
这是例外:
Exception in thread "JavaFX Application Thread" java.lang.ClassCastException: javafx.scene.control.Button cannot be cast to application.Main$Sprite
at application.Main.lambda$0(Main.java:193)
at java.util.Collection.removeIf(Unknown Source)
at application.Main.update(Main.java:191)
at application.Main.access$0(Main.java:91)
at application.Main$1.handle(Main.java:53)
at javafx.animation.AnimationTimer$AnimationTimerReceiver.lambda$handle$0(AnimationTimer.java:57)
at java.security.AccessController.doPrivileged(Native Method)
at javafx.animation.AnimationTimer$AnimationTimerReceiver.handle(AnimationTimer.java:56)
at com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(AbstractMasterTimer.java:357)
at com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(AbstractMasterTimer.java:267)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:557)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:541)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:534)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$11(QuantumToolkit.java:340)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
at java.lang.Thread.run(Unknown Source)
目标是当您单击按钮时,游戏将重新启动并以与以前相同的方式运行。
希望您能为我解决问题,并先谢谢您。
巴伐利亚的问候
Christoph
答案 0 :(得分:1)
问题出在您的update
方法中:
root.getChildren().removeIf( n -> {
Sprite s = (Sprite) n;
return s.dead;
});
您只是假设getChildren
返回的所有元素都是Sprite
,但是该异常指出其中至少有一个javafx.scene.control.Button
。
如果只想删除死掉的精灵,请使用以下方法:
root.getChildren().removeIf(n -> n instanceof Sprite && ((Sprite)n).dead);
作为对未来的暗示:学习阅读stacktrace。大多数时候,他们会告诉您问题出在哪里。