| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 编程论坛
共有 316 人关注过本帖
标题:java.lang.NullPointerException未知错误??
只看楼主 加入收藏
渐渐鱼
Rank: 1
等 级:新手上路
帖 子:74
专家分:0
注 册:2018-5-11
结帖率:100%
  已结贴   问题点数:20  回复次数:8   
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);
    }
}


蛇不能吃食物???
求大神解!!!!

月月姐在吗??@林月儿
2018-10-03 19:07
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:104
帖 子:1667
专家分:7280
注 册:2015-3-19
  得分:0 
没开电脑,等吃完饭帮你看一下

wechat    PrinceThumb
2018-10-03 22:26
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:104
帖 子:1667
专家分:7280
注 册:2015-3-19
  得分:0 
问题蛮多的,变量的命名规范,作用域混淆,代码的缩进,还有一些笔误等等
对自己要求还是要高一点啊!
对了,这个资源关闭没查,stop()好像是能监听到窗体关闭的。你可以查查看确认下
程序代码:
package application2;

import java.util.Arrays;
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.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;
import javafx.stage.Stage;
import javafx.util.Duration;

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> foods = 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);
            foods.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();
            }
        }
        Move move = new Move();
        //监听键盘事件
        scene.setOnKeyReleased(new EventHandler<KeyEvent>()
        {
            public void handle(KeyEvent event)
            {
                if(Arrays.asList("W", "w", "A", "a",
                        "S", "s", "D", "d").contains(event.getText())){
                    keyCode = event.getText();
                }
            }//hanndle
        });//listen
        Timer timer = new Timer();
        timer.schedule(new TimerTask()
        {
            public void run()
            {
                if(keyCode == null)
                    return;
                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(arcValue != null) {
                    move.moveObj(arcValue,arcMove);
                }
            }
        } ,10,100);
        
        //吃食物
        Timer foodTimer = new Timer();
        foodTimer.schedule(new TimerTask()
        {
            public void run()
            {
                double arcX = arc.getCenterX();
                double arcY = arc.getCenterY();
                for(Circle cir : new LinkedList<>(foods))
                {
                    double cirX = cir.getCenterX();
                    double cirY = cir.getCenterY();
                    double distance = Math.sqrt((arcX-cirX)*(arcX-cirX)+(arcY-cirY)*(arcY-cirY));
                    if(distance < 30)
                    {
                        list.add(cir);
                        foods.remove(cir);
                    }
                }
            }
        } ,10,1000);
        g.getChildren().addAll(list);
        g.getChildren().addAll(foods);
        g.getChildren().add(arc);
        
        stage.setScene(scene);
        stage.show();
    }
    @Override
    public void stop() throws Exception {
        System.exit(0);
    }
    public static void main(String[] args) {
        launch(args);
    }
}


[此贴子已经被作者于2018-10-3 23:21编辑过]


wechat    PrinceThumb
2018-10-03 23:20
渐渐鱼
Rank: 1
等 级:新手上路
帖 子:74
专家分:0
注 册:2018-5-11
  得分:0 
回复 3楼 林月儿
月月姐,先前的问题都修改好了,谢谢你的指导。最后还有一个问题,新建了BEGIN类作为开始页面和Again的游戏结束重新开始界面。这两个页面通过Button按钮来监听事件达到转换界面的效果,但是main类也就是游戏的那个要监听arc这个圆弧的坐标来控制页面的转换。这个事件.....额额额,找了好多资料都没办法解决姐姐有什么好的思路可以解决这个问题吗?
2018-10-05 16:47
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:104
帖 子:1667
专家分:7280
注 册:2015-3-19
  得分:0 
你还在搞这个游戏啊?那试试这个吧
程序代码:
package application2;

import javax.swing.JOptionPane;

public class Begin {
    public static void main(String[] args) {
        if(JOptionPane.showConfirmDialog(null, "开始游戏?")
                == JOptionPane.OK_OPTION) {
            Main.main(null);
        }
    }
}

Again的游戏结束重新开始界面,不确定你的结束条件什么。
至于怎么处理,你得描述清楚。
除了先前的问题,下面这个比较明显的方向问题也要改掉
Timer timer = new Timer();
timer.schedule(new TimerTask()
{
    public void run()
    {
        if(keyCode == null)
            return;
        DoubleProperty arcValue = null;
        double arcMove = 0;
        
        switch(keyCode)
        {
        case "W":
        case "w":
            arc.setRotate(-90);
            arcValue = arc.centerYProperty();
            arcMove = arc.getCenterY()-30;
            break;
        case "A":
        case "a":
            arc.setRotate(180);
            arcValue = arc.centerXProperty();
            arcMove = arc.getCenterX()-30;
            break;
        case "S":
        case "s":
            arc.setRotate(90);
            arcValue = arc.centerYProperty();
            arcMove = arc.getCenterY()+30;
            break;
            
        case "D":
        case "d":
            arcValue = arc.centerXProperty();
            arcMove = arc.getCenterX()+30;
            break;
        }
        if(arcValue != null) {
            move.moveObj(arcValue,arcMove);
        }
    }
} ,10,100);

你这个尾插法感觉挺新颖,之前c板块wmf2014有个头插法的有兴趣可以研究一下
https://bbs.bccn.net/thread-442229-1-1.html
附件: 您没有浏览附件的权限,请 登录注册

wechat    PrinceThumb
2018-10-05 18:29
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:104
帖 子:1667
专家分:7280
注 册:2015-3-19
  得分:20 
我该吃饭了,先下啦

wechat    PrinceThumb
2018-10-05 18:37
渐渐鱼
Rank: 1
等 级:新手上路
帖 子:74
专家分:0
注 册:2018-5-11
  得分:0 
回复 6楼 林月儿
月月姐,之前说的那个问题我再重新描述一下。

就是一共三个界面:begin,again和main。

begin是开始界面。就是从begin界面可以进入main游戏类里。

然后main游戏进行过程中如果死亡就结束游戏然后要跳转到again界面。again里有一个按钮可以再次重新开始游戏。

现在begin到main的跳转和again再返回到main都没问题。通过按钮来控制。

但是main里死亡需要通过arc也就是蛇头的坐标来判断,监听蛇头的坐标,如果出了边界,就跳转到again类。这个步骤的实现没有完成。

因为begin和again都是通过按钮来控制跳转,这个就通过坐标 。。鹅鹅鹅。事件处理里似乎没有这个

有什么好的idea没??
2018-10-06 13:30
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:104
帖 子:1667
专家分:7280
注 册:2015-3-19
  得分:0 
那就按坐标判断啊,src不是有坐标属性吗?
首先判断位置你可以考虑定时任务,因为考虑到游戏进度的监控么
判断条件不就是src的currentx,currenty属性哪~
想多了吧小伙子?

wechat    PrinceThumb
2018-10-06 14:08
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:104
帖 子:1667
专家分:7280
注 册:2015-3-19
  得分:0 
你这个20分就给到我回复去吃饭了

wechat    PrinceThumb
2018-10-06 14:09







关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.039002 second(s), 8 queries.
Copyright©2004-2018, BCCN.NET, All Rights Reserved