游戏启动后,滴答方法崩溃

时间:2018-08-29 22:13:58

标签: java eclipse

这个想法是有一个计时器,它是2000滴答声(2秒),并且每次达到2000的倍数(例如2、4、6、8)时,它将在我的游戏中为敌人生成一个实体。该实体是一颗子弹。我将其余代码放置在一个保管箱中,因为它会造成混乱。我正在使用Java Eclipse IDE编写游戏代码。 https://www.dropbox.com/sh/kuvoxzjf00wa8jf/AAD-MaXXcnHMn4PtW-X_vHcUa?dl=0

这是我得到的错误:

Exception in thread "Thread-0" java.lang.NullPointerException
    at com.Game.src.main.Game.tick(Game.java:150)
    at com.Game.src.main.Game.run(Game.java:126)
    at java.base/java.lang.Thread.run(Unknown Source)

该错误在游戏启动后以及单击“播放”按钮时发生。这就是发生错误的地方。第126行:public void run()中的tick()和第150行:e.tick()。我放在代码private Enemy e的顶部;跟踪敌人的位置,以便敌人的子弹可以发射。

public void run(){ //Game Loop - Heart of Game 
            init();
            long lastTime = System.nanoTime(); // Long is the same as an int just stores a higher pos number and a lower neg number
            final double amountOfTicks = 60.0;
            double ns = 1000000000 / amountOfTicks;
            double delta = 0; //calculates time past to catch up; the number will increase
            //^ Will only get ran once 
            int updates = 0;
            int frames = 0;
            long timer = System.currentTimeMillis(); //Milliseconds
            while(running){
                long now = System.nanoTime(); //takes time to get from long lastTime to this line
                delta += (now - lastTime) / ns;
                lastTime = now;
                if(delta >= 1){
                    try {
                        tick();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    updates++;
                    delta--;
                }
                render();
                frames++;

                if(System.currentTimeMillis() - timer > 1000){
                    timer += 1000;
                    System.out.println(updates + "Ticks, Fps " + frames);
                    updates = 0;
                    frames = 0;
                }
            }
            stop();

         }
         private void tick() throws InterruptedException{ //everything that updates
            if(State == STATE.GAME) {

            e.tick();
            p.tick();
            c.tick();

            Instant start = Instant.now();

            Thread.sleep(2000);

            Instant end = Instant.now();

            Duration elapsed = Duration.between(start, end);

            if(elapsed.toMillis() >= 2000) {
                c.addEntity(new EnemyBullet(e.getX(), e.getY(), tex, this));
            }
            }

            if(enemy_killed >= enemy_count) {
                enemy_count = 5;
                enemy_killed = 0;
                c.createEnemy(enemy_count);
            }

         }

0 个答案:

没有答案