Javafx出现意外错误

时间:2018-02-19 03:40:06

标签: java animation javafx 2d

大家好! 我在这里再次惹恼你)

所以,我试图制作游戏,我描述了一些类:     GameObject - 只是一个具有X,Y,Sizes,Name的对象,可以绘制和移动:

package pkg3dgraphics;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;


public class GameObject {
    protected String name;
    protected int xCoord,yCoord,mySizeX = 25,mySizeY = 25;
    public GameObject(){

    }
    public GameObject(String newName, int startXCoord, int startYCoord){
        setName(newName);
        setX(startXCoord);
        setY(startYCoord);
    }
    public void SetSize(int X, int Y){
        mySizeX = X;
        mySizeY = Y;
    }
    public int getXsize(){
        return mySizeX;
    }
    public int getYsize(){
           return mySizeY;
    }
    public String getName(){
        return name;
    }
    public void setName(String newName){
        name = newName;
    }
    public int getX(){
        return xCoord;
    }
     public int getY(){
        return yCoord;
    }
    public void setX(int newXCoord){
        xCoord  = newXCoord;
    }

    public void setY(int newYCoord){
        yCoord  = newYCoord;
    }
    public void moveTo(int X,int Y){
     xCoord  = X;
     yCoord  = Y;
    }
    public void moveBy(int X,int Y){
     xCoord +=X;
     yCoord +=Y;
    }
   public  void Draw(GraphicsContext currentContext){
         currentContext.setFill(Color.GREEN);
         currentContext.setStroke(Color.BLUE);
         currentContext.fillOval(xCoord,yCoord, mySizeX,mySizeY );

    }
}

我让Shooter扩展了上一堂课,他在这里:

    package pkg3dgraphics;


    public class Shooter extends GameObject {
        public Shooter(){

        }
        public Shooter(String newName, int startXCoord, int startYCoord){
            setName(newName);
            setX(startXCoord);
            setY(startYCoord);
        }
        public Bullet shoot(String direction){
            Bullet newBullet = new Bullet(direction);
            return newBullet;
        }

    }

我还有子弹,你知道,子弹:

    package pkg3dgraphics;

    import javafx.scene.paint.Color;
    import javafx.scene.canvas.GraphicsContext;

    public class Bullet extends GameObject{
        String myDirection;
        protected int mySpeed = 5,mySizeX  = 5,mySizeY = 5;
        boolean  goNorth,goSouth,goWest,goEast;
        protected boolean active = false;
        public void setSpeed(int newSpeed){
            mySpeed = newSpeed;
        }
        public void setDirection(String newDirection){
            myDirection = newDirection;
            active = true;
            if ( myDirection == "North" )
                goNorth = true;
            if ( myDirection == "South" )
                goSouth = true;
            if ( myDirection == "West" )
                goWest = true;
            if ( myDirection == "East" )
                goEast = true;
        }
        Bullet(String direction ){
            myDirection = direction;
            active = true;
            if ( myDirection == "North" )
                goNorth = true;
            if ( myDirection == "South" )
                goSouth = true;
            if ( myDirection == "West" )
                goWest = true;
            if ( myDirection == "East" )
                goEast = true;      
        }

        public void advance(int W,int H,GraphicsContext gc){
           if (xCoord <= W && xCoord >= 0 && yCoord <= H && yCoord >= 0 ) {
                    if (goNorth) moveBy(0,mySpeed);
                    if (goSouth) moveBy(0,mySpeed);
                    if (goWest) moveBy(mySpeed,0);
                    if (goEast) moveBy(mySpeed,0);
           }else{
               active = false;
               Vanish(gc);
               goNorth = false;
               goSouth = false;
               goWest  = false;
               goEast  = false;
           }
        }
        public void Vanish(GraphicsContext gc){
            gc.setFill(Color.WHITE);
        }
    }

目的是让这个家伙以下一个方式开枪: 当我按下按钮时,我使用预先制作的非活动子弹并将其指向几个方向。 当这个子弹穿过窗户的边界时,它变为不活动并停止。 为此,我有一系列项目符号。 当用户按下射击键时,我在阵列中寻找一个非活动的子弹,如果没有,我会创建另一个跟随用户想要的路径。 好吧,这种实施子弹的方式可能不是最好的,但我没有想出另一个。

所以我编译了我的主类:

package pkg3dgraphics;

import java.util.Vector;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import static javafx.scene.input.KeyCode.DOWN;
import static javafx.scene.input.KeyCode.LEFT;
import static javafx.scene.input.KeyCode.RIGHT;
import static javafx.scene.input.KeyCode.SHIFT;
import static javafx.scene.input.KeyCode.UP;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;






public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }



    public int WINDOWWIDTH = 600;
    public int WINDOWHEIGHT = 400;
    boolean goNorth,goSouth,goWest,goEast,running,ShootNorth,ShootSouth,ShootWest,ShootEast;
    Shooter player = new Shooter("Player",WINDOWWIDTH/2,WINDOWHEIGHT/2);
    Bullet bullets[]  = new Bullet[500];
    int bulletsSize = 0;
     Canvas mainCanvas = new Canvas(WINDOWWIDTH,WINDOWHEIGHT);
     GraphicsContext mainContext =  mainCanvas.getGraphicsContext2D();
    @Override 
    public void start(Stage primaryStage) {
        Group root = new Group();
        Canvas mainCanvas = new Canvas(WINDOWWIDTH,WINDOWHEIGHT);
        GraphicsContext mainContext =  mainCanvas.getGraphicsContext2D();
        root.getChildren().add(mainCanvas);

        Scene scene  = new Scene(root,WINDOWWIDTH,WINDOWHEIGHT);
        scene.setOnKeyPressed(new EventHandler<KeyEvent> (){
            @Override
            public void handle(KeyEvent event){
                        switch(event.getCode()){
                            case UP:        ShootNorth = true; break;
                            case DOWN:      ShootSouth = true; break;
                            case LEFT:      ShootWest  = true; break;
                            case RIGHT:     ShootEast  = true; break;
                            case W:         goNorth = true; break;
                            case S:         goSouth = true; break;
                            case A:         goWest  = true; break;
                            case D:         goEast  = true; break;
                        } 
            }
        });
       scene.setOnKeyReleased(new EventHandler <KeyEvent>(){
           @Override
           public void handle(KeyEvent event){
               switch(event.getCode()){
                   case UP:         ShootNorth = false; break;
                   case DOWN:       ShootSouth = false; break;
                   case LEFT:       ShootWest  = false; break;
                   case RIGHT:      ShootEast  = false; break;
                   case W:          goNorth = false; break;
                   case S:          goSouth = false; break;
                   case A:          goWest  = false; break;
                   case D:          goEast  = false; break;

               }
           }
       }); 
        primaryStage.setScene(scene);
        primaryStage.show();
       AnimationTimer Timer = new AnimationTimer(){
           @Override
           public void handle (long now){
               int dx = 0, dy = 0;
               if (goNorth) dy = -1;
               if (goSouth) dy = 1;
               if (goWest)  dx = -1;
               if (goEast)  dx = 1;
               if (running) { dx *= 3; dy *= 3;}
               mainContext.clearRect(0,0,WINDOWWIDTH,WINDOWHEIGHT);
               player.moveBy(dx, dy);
               CheckShoot();
               player.Draw(mainContext);

           }


       };
       Timer.start();


    }

    public void CheckShoot(){
        String direction = null;
        int count = 0;
        if (ShootNorth)
            {
                direction = "North";
            }
        if (ShootSouth)
            {
                direction = "South";
            }
        if (ShootWest)
            {
                direction = "West";
            }
        if (ShootEast)
            {
                direction = "East";
            }
        for (int i = 0; i < bulletsSize; i ++ )
        {
            if (bullets[i].active = false ){
                bullets[i].setDirection(direction);
                bullets[i].moveTo(player.getX(),player.getY());
                break;
            }else count ++;
        }
        if ( count == bulletsSize ) {
           bulletsSize++;
           bullets[bulletsSize] = player.shoot(direction);
        }

    }

     public void advanceAll(){
        for (int i = 0; i < bulletsSize; i ++ )
        {
            bullets[i].advance(WINDOWWIDTH,WINDOWHEIGHT,mainContext);
        }
    }

}

应用程序运行但后来我解决了这个问题,直到关闭应用程序:

   Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
        at pkg3dgraphics.Main.CheckShoot(Main.java:126)
        at pkg3dgraphics.Main$3.handle(Main.java:91)
        at javafx.animation.AnimationTimer$AnimationTimerReceiver.lambda$handle$483(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:506)
        at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:490)
        at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$403(QuantumToolkit.java:319)
        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$147(WinApplication.java:177)
        at java.lang.Thread.run(Thread.java:748)

所以,这就是我想抱怨的全部内容。 如果有人知道为什么这些都会发生,如果你和我分享这个秘密,我会很高兴和感激。(和所有其他人一样)

1 个答案:

答案 0 :(得分:0)

您好像已经创建了数组,而没有创建每个成员

Bullet bullets[]  = new Bullet[500];

然后你迭代它们,而它们是null s

    for (int i = 0; i < bulletsSize; i ++ )
    {
        if (bullets[i].active = false ){
            bullets[i].setDirection(direction);
            bullets[i].moveTo(player.getX(),player.getY());
            break;
        }else count ++;
    }

bullets[i] = null,您尝试在其上调用.active

解决方案:

尝试检查空值

        if (bullets[i] == null) { bullets[i] = new Bullet(); }
        if (bullets[i].active = false ){
            bullets[i].setDirection(direction);
            bullets[i].moveTo(player.getX(),player.getY());
            break;
        }else count ++;

阅读What is NullPointerException