javaFX的timeline时间轴循环??贪吃蛇
程序代码:
package application; import java.util.Iterator; import java.util.LinkedList; import javafx.animation.KeyFrame; import javafx.animation.KeyValue; import javafx.animation.Timeline; import javafx.application.Application; import javafx.event.EventHandler; import javafx.stage.Stage; import javafx.util.Duration; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.input.KeyEvent; import javafx.scene.paint.Color; import javafx.scene.shape.Arc; import javafx.scene.shape.ArcType; import javafx.scene.shape.Circle; public class Main extends Application { @Override public void start(Stage stage) { Group g = new Group(); Scene scene = new Scene(g,600,600); //画蛇 LinkedList<Circle> list = new LinkedList<>(); for(int i=0;i<5;i++) { Circle cir = new Circle(); cir.setCenterX(275-35*i); cir.setCenterY(200); cir.setRadius(20); cir.setFill(Color.rgb(0, 255 ,127)); list.add(cir); } Arc arc = new Arc(); arc.setFill(Color.rgb(0, 255 ,127) ); arc.setCenterX(310); arc.setCenterY(200); arc.setRadiusX(20); arc.setRadiusY(20); arc.setStartAngle(45); arc.setLength(270); arc.setType(ArcType.ROUND); class Move { public void A() { System.out.println("A"); Timeline timeline=new Timeline(); KeyValue ARCxValue = new KeyValue(arc.centerXProperty(),arc.getCenterX()-50); KeyFrame ARCkeyFrame=new KeyFrame(Duration.millis(1000), ARCxValue); timeline.getKeyFrames().add(ARCkeyFrame); //body Iterator<Circle> iterator = list.iterator(); Circle cir1 = iterator.next(); KeyValue cir1xValue = new KeyValue(cir1.centerXProperty(),arc.getCenterX()); KeyValue cir1yValue = new KeyValue(cir1.centerYProperty(),arc.getCenterY()); KeyFrame keyFrame1=new KeyFrame(Duration.millis(1000), cir1xValue,cir1yValue); timeline.getKeyFrames().add(keyFrame1); Circle cir3 = iterator.next(); KeyValue cir3xValue = new KeyValue(cir3.centerXProperty(),cir1.getCenterX()); KeyValue cir3yValue = new KeyValue(cir3.centerYProperty(),cir1.getCenterY()); KeyFrame keyFrame2=new KeyFrame(Duration.millis(1000), cir3xValue,cir3yValue); timeline.getKeyFrames().add(keyFrame2); Circle cir4 = iterator.next(); KeyValue cir4xValue = new KeyValue(cir4.centerXProperty(),cir3.getCenterX()); KeyValue cir4yValue = new KeyValue(cir4.centerYProperty(),cir3.getCenterY()); KeyFrame keyFrame3=new KeyFrame(Duration.millis(1000), cir4xValue,cir4yValue); timeline.getKeyFrames().add(keyFrame3); Circle cir5 = iterator.next(); KeyValue cir5xValue = new KeyValue(cir5.centerXProperty(),cir4.getCenterX()); KeyValue cir5yValue = new KeyValue(cir5.centerYProperty(),cir4.getCenterY()); KeyFrame keyFrame4=new KeyFrame(Duration.millis(1000), cir5xValue,cir5yValue); timeline.getKeyFrames().add(keyFrame4); Circle cir6 = iterator.next(); KeyValue cir6xValue = new KeyValue(cir6.centerXProperty(),cir5.getCenterX()); KeyValue cir6yValue = new KeyValue(cir6.centerYProperty(),cir5.getCenterY()); KeyFrame keyFrame5=new KeyFrame(Duration.millis(1000), cir6xValue,cir6yValue); timeline.getKeyFrames().add(keyFrame5); timeline.play(); } public void W() { System.out.println("W"); Timeline timeline=new Timeline(); //head KeyValue ARCyValue = new KeyValue(arc.centerYProperty(),arc.getCenterY()-50); KeyFrame ARCkeyFrame=new KeyFrame(Duration.millis(1000), ARCyValue); timeline.getKeyFrames().add(ARCkeyFrame); //body Iterator<Circle> iterator = list.iterator(); Circle cir1 = iterator.next(); KeyValue cir1xValue = new KeyValue(cir1.centerXProperty(),arc.getCenterX()); KeyValue cir1yValue = new KeyValue(cir1.centerYProperty(),arc.getCenterY()); KeyFrame keyFrame1=new KeyFrame(Duration.millis(1000), cir1xValue,cir1yValue); timeline.getKeyFrames().add(keyFrame1); Circle cir3 = iterator.next(); KeyValue cir3xValue = new KeyValue(cir3.centerXProperty(),cir1.getCenterX()); KeyValue cir3yValue = new KeyValue(cir3.centerYProperty(),cir1.getCenterY()); KeyFrame keyFrame2=new KeyFrame(Duration.millis(1000), cir3xValue,cir3yValue); timeline.getKeyFrames().add(keyFrame2); Circle cir4 = iterator.next(); KeyValue cir4xValue = new KeyValue(cir4.centerXProperty(),cir3.getCenterX()); KeyValue cir4yValue = new KeyValue(cir4.centerYProperty(),cir3.getCenterY()); KeyFrame keyFrame3=new KeyFrame(Duration.millis(1000), cir4xValue,cir4yValue); timeline.getKeyFrames().add(keyFrame3); Circle cir5 = iterator.next(); KeyValue cir5xValue = new KeyValue(cir5.centerXProperty(),cir4.getCenterX()); KeyValue cir5yValue = new KeyValue(cir5.centerYProperty(),cir4.getCenterY()); KeyFrame keyFrame4=new KeyFrame(Duration.millis(1000), cir5xValue,cir5yValue); timeline.getKeyFrames().add(keyFrame4); Circle cir6 = iterator.next(); KeyValue cir6xValue = new KeyValue(cir6.centerXProperty(),cir5.getCenterX()); KeyValue cir6yValue = new KeyValue(cir6.centerYProperty(),cir5.getCenterY()); KeyFrame keyFrame5=new KeyFrame(Duration.millis(1000), cir6xValue,cir6yValue); timeline.getKeyFrames().add(keyFrame5); timeline.play(); } public void S() { System.out.println("S"); Timeline timeline=new Timeline(); KeyValue ARCyValue = new KeyValue(arc.centerYProperty(),arc.getCenterY()+50); KeyFrame ARCkeyFrame=new KeyFrame(Duration.millis(1000), ARCyValue); timeline.getKeyFrames().add(ARCkeyFrame); //body Iterator<Circle> iterator = list.iterator(); Circle cir1 = iterator.next(); KeyValue cir1xValue = new KeyValue(cir1.centerXProperty(),arc.getCenterX()); KeyValue cir1yValue = new KeyValue(cir1.centerYProperty(),arc.getCenterY()); KeyFrame keyFrame1=new KeyFrame(Duration.millis(1000), cir1xValue,cir1yValue); timeline.getKeyFrames().add(keyFrame1); Circle cir3 = iterator.next(); KeyValue cir3xValue = new KeyValue(cir3.centerXProperty(),cir1.getCenterX()); KeyValue cir3yValue = new KeyValue(cir3.centerYProperty(),cir1.getCenterY()); KeyFrame keyFrame2=new KeyFrame(Duration.millis(1000), cir3xValue,cir3yValue); timeline.getKeyFrames().add(keyFrame2); Circle cir4 = iterator.next(); KeyValue cir4xValue = new KeyValue(cir4.centerXProperty(),cir3.getCenterX()); KeyValue cir4yValue = new KeyValue(cir4.centerYProperty(),cir3.getCenterY()); KeyFrame keyFrame3=new KeyFrame(Duration.millis(1000), cir4xValue,cir4yValue); timeline.getKeyFrames().add(keyFrame3); Circle cir5 = iterator.next(); KeyValue cir5xValue = new KeyValue(cir5.centerXProperty(),cir4.getCenterX()); KeyValue cir5yValue = new KeyValue(cir5.centerYProperty(),cir4.getCenterY()); KeyFrame keyFrame4=new KeyFrame(Duration.millis(1000), cir5xValue,cir5yValue); timeline.getKeyFrames().add(keyFrame4); Circle cir6 = iterator.next(); KeyValue cir6xValue = new KeyValue(cir6.centerXProperty(),cir5.getCenterX()); KeyValue cir6yValue = new KeyValue(cir6.centerYProperty(),cir5.getCenterY()); KeyFrame keyFrame5=new KeyFrame(Duration.millis(1000), cir6xValue,cir6yValue); timeline.getKeyFrames().add(keyFrame5); timeline.play(); } public void D() { System.out.println("D"); Timeline timeline=new Timeline(); KeyValue ARCxValue = new KeyValue(arc.centerXProperty(),arc.getCenterX()+50); KeyFrame ARCkeyFrame=new KeyFrame(Duration.millis(1000), ARCxValue); timeline.getKeyFrames().add(ARCkeyFrame); //body Iterator<Circle> iterator = list.iterator(); Circle cir1 = iterator.next(); KeyValue cir1xValue = new KeyValue(cir1.centerXProperty(),arc.getCenterX()); KeyValue cir1yValue = new KeyValue(cir1.centerYProperty(),arc.getCenterY()); KeyFrame keyFrame1=new KeyFrame(Duration.millis(1000), cir1xValue,cir1yValue); timeline.getKeyFrames().add(keyFrame1); Circle cir3 = iterator.next(); KeyValue cir3xValue = new KeyValue(cir3.centerXProperty(),cir1.getCenterX()); KeyValue cir3yValue = new KeyValue(cir3.centerYProperty(),cir1.getCenterY()); KeyFrame keyFrame2=new KeyFrame(Duration.millis(1000), cir3xValue,cir3yValue); timeline.getKeyFrames().add(keyFrame2); Circle cir4 = iterator.next(); KeyValue cir4xValue = new KeyValue(cir4.centerXProperty(),cir3.getCenterX()); KeyValue cir4yValue = new KeyValue(cir4.centerYProperty(),cir3.getCenterY()); KeyFrame keyFrame3=new KeyFrame(Duration.millis(1000), cir4xValue,cir4yValue); timeline.getKeyFrames().add(keyFrame3); Circle cir5 = iterator.next(); KeyValue cir5xValue = new KeyValue(cir5.centerXProperty(),cir4.getCenterX()); KeyValue cir5yValue = new KeyValue(cir5.centerYProperty(),cir4.getCenterY()); KeyFrame keyFrame4=new KeyFrame(Duration.millis(1000), cir5xValue,cir5yValue); timeline.getKeyFrames().add(keyFrame4); Circle cir6 = iterator.next(); KeyValue cir6xValue = new KeyValue(cir6.centerXProperty(),cir5.getCenterX()); KeyValue cir6yValue = new KeyValue(cir6.centerYProperty(),cir5.getCenterY()); KeyFrame keyFrame5=new KeyFrame(Duration.millis(1000), cir6xValue,cir6yValue); timeline.getKeyFrames().add(keyFrame5); timeline.play(); } } //监听键盘事件 scene.setOnKeyReleased(new EventHandler<KeyEvent>() { public void handle(KeyEvent event) { Move move = new Move(); switch(event.getCode()) { case W: { move.W(); break; } case S: { move.S(); break; } case A: { move.A(); break; } case D: { move.D(); break; } } } } ); g.getChildren().addAll(list); g.getChildren().add(arc); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
做的一个贪吃蛇的小游戏,设置wasd四个按键只能单步实现移动,加while循环似乎不行
求问:怎么实现按下一个键可以一直执行单步的循环移动???