java.lang.NullPointerException未知错误??
程序代码:
package application; import java.util.LinkedList; import java.util.Random; import java.util.Timer; import java.util.TimerTask; import javafx.animation.KeyFrame; import javafx.animation.KeyValue; import javafx.animation.Timeline; import javafx.application.Application; import javafx.beans.property.DoubleProperty; 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 { static String keyCode =null; @Override public void start(Stage stage) { Group g = new Group(); Scene scene = new Scene(g,1024,700); LinkedList<Circle> list = new LinkedList<>(); LinkedList<Circle> list1 = new LinkedList<>(); //画食物 Random r = new Random(); for(int i=0;i<10;i++) { int x =r.nextInt(1009-15+1)+15; int y =r.nextInt(685-15+1)+15; Circle cir = new Circle(); cir.setCenterX(x); cir.setCenterY(y); cir.setRadius(15); cir.setFill(Color.BLACK); list1.add(cir); } //画蛇 Arc arc = new Arc(); arc.setFill(Color.BLACK); arc.setCenterX(300); arc.setCenterY(200); arc.setRadiusX(15); arc.setRadiusY(15); arc.setStartAngle(45); arc.setLength(270); arc.setType(ArcType.ROUND); for(int i=0;i<5;i++) { Circle cir = new Circle(); cir.setCenterX(270-30*i); cir.setCenterY(200); cir.setRadius(15); cir.setFill(Color.BLACK); list.add(cir); } //定义移动 class Move { public void moveObj(DoubleProperty arcValue, double arcMove) { Timeline timeline=new Timeline(); //head KeyValue ARCValue = new KeyValue(arcValue,arcMove); KeyFrame ARCkeyFrame = new KeyFrame(Duration.millis(500),ARCValue); timeline.getKeyFrames().add(ARCkeyFrame); //body double preX=arc.getCenterX(); double preY=arc.getCenterY(); boolean isFirst=true; for(int i=0;i<list.size();i++) { if(!isFirst) { preX=list.get(i-1).getCenterX(); preY=list.get(i-1).getCenterY(); } KeyValue xValue= new KeyValue(list.get(i).centerXProperty(),preX); KeyValue yValue= new KeyValue(list.get(i).centerYProperty(),preY); KeyFrame keyFrame = new KeyFrame(Duration.millis(500),xValue,yValue); timeline.getKeyFrames().add(keyFrame); isFirst=false; } timeline.play(); } } //监听键盘事件 scene.setOnKeyReleased(new EventHandler<KeyEvent>() { public void handle(KeyEvent event) { keyCode=event.getText(); DoubleProperty arcValue=null; double arcMove = 0; switch(keyCode) { case "W": case "w": arcValue=arc.centerYProperty(); arcMove=arc.getCenterY()-30; break; case "A": case "a": arcValue=arc.centerXProperty(); arcMove=arc.getCenterX()-30; break; case "S": case "s": arcValue=arc.centerYProperty(); arcMove=arc.getCenterY()+30; break; case "D": case "d": arcValue=arc.centerXProperty(); arcMove=arc.getCenterX()+30; break; } Move move = new Move(); move.moveObj(arcValue,arcMove); Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { DoubleProperty arcValue=null; double arcMove = 0; switch(keyCode) { case "W": case "w": arcValue=arc.centerYProperty(); arcMove=arc.getCenterY()-30; break; case "A": case "a": arcValue=arc.centerXProperty(); arcMove=arc.getCenterX()-30; break; case "S": case "s": arcValue=arc.centerYProperty(); arcMove=arc.getCenterY()+30; break; case "D": case "d": arcValue=arc.centerXProperty(); arcMove=arc.getCenterX()+30; break; } if(event.getText()!=null) { move.moveObj(arcValue,arcMove); } } } ,10,100); //吃食物 Timer timer1 = new Timer(); timer1.schedule(new TimerTask() { double arcX=arc.getCenterX(); double arcY=arc.getCenterY(); public void run() { for(int i=0;i<list1.size();i++) { double cirX=list1.get(i).getCenterX(); double cirY=list1.get(i).getCenterX(); double distance=Math.sqrt((arcX-cirX)*(arcX-cirX)+(arcY-cirY)*(arcY-cirY)); if(distance<30) { list.add(list1.get(i)); list1.remove(i); } } } } ,10,100); }//hanndle }//listen ); g.getChildren().addAll(list); g.getChildren().addAll(list1); g.getChildren().add(arc); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
蛇不能吃食物???
求大神解!!!!
月月姐在吗??@林月儿