2016-12-03 9 views
0

tick()render()を試してみると、私のゲームクラスの他のクラスは動作しません。ダニとレンダーが他のクラスのために働いていない

Exception in thread "Thread-2" java.lang.NullPointerException 
    at ca.patrick.main.window.Game.render(Game.java:87) 
    at ca.patrick.main.window.Game.run(Game.java:52) 
    at java.lang.Thread.run(Unknown Source) 

、時には:

Exception in thread "Thread-2" java.lang.NullPointerException 
    at ca.patrick.main.window.Game.tick(Game.java:71) 
    at ca.patrick.main.window.Game.run(Game.java:54) 
    at java.lang.Thread.run(Unknown Source) 

は、だからここに私のゲームのためのコード(私は「名前」で私の名前を置き換え)と あなたが見ることができるように私は、Javaの初心者ですので、私のコードですおそらく、サブ標準ですが、ここに私のコードです:

package ca.name.main.window; 

import java.awt.Canvas; 
import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.image.BufferStrategy; 
import java.util.LinkedList; 

import ca.name.main.framework.GameObject; 
import ca.name.main.framework.Id; 
import ca.name.main.objects.Enemy1; 

public class Game extends Canvas implements Runnable{ 

    private static final long serialVersionUID = -4319386659730073928L; 

    public int WIDTH, HEIGHT; 
    public boolean running = false; 
    private Thread thread; 

    private LinkedList<GameObject> object; 
    private Enemy1 enemy1; 

    public synchronized void start(){ 
     if(running) 
      return; 

     running = true; 
     thread = new Thread(this); 
     thread.start(); 
    } 

ゲームループ:

public void run() { 
     init(); 
     this.requestFocus(); 
     long lastTime = System.nanoTime(); 
     double amountOfTicks = 60.0; 
     double ns = 1000000000/amountOfTicks; 
     double delta = 0; 
     long timer = System.currentTimeMillis(); 
     int updates = 0; 
     int frames = 0; 
     while (running) { 
      long now = System.nanoTime(); 
      delta += (now - lastTime)/ns; 
      lastTime = now; 
      while (delta >= 1) { 
       tick(); 
       updates++; 
       delta--; 
      } 
      render(); 
      frames++; 

      if (System.currentTimeMillis() - timer > 1000) { 
       timer += 1000; 
       System.out.println("FPS: " + frames + " TICKS: " + updates); 
       frames = 0; 
       updates = 0; 
      } 
     } 
    } 

    private void init() { 
     HEIGHT = getHeight(); 
     WIDTH = getWidth(); 

     Enemy1 enemy1 = new Enemy1(0, 0, Id.Enemy); 
    } 

ダニ:

private void tick() { 
     enemy1.tick(object); 
    } 

レンダリング:

private void render() { 
     BufferStrategy bs = this.getBufferStrategy(); 
     if(bs == null){ 
      this.createBufferStrategy(3); 
      return; 
     } 

     Graphics g = bs.getDrawGraphics(); 

     g.setColor(Color.cyan); 
     g.fillRect(0, 0, getHeight() * 2, getWidth() * 2); 

     enemy1.render(g); 

     bs.show(); 
     g.dispose(); 
    } 

    public Game(){ 

    } 

    public static void main(String args[]){ 
     new Window(600, 800, "Log Platformer prototype", new Game()); 
    } 
} 

答えて

1

を私はちょうどここに私のコードです:)

package ca.patrick.main.window; 

import java.awt.Canvas; 
import java.awt.Color; 
import java.awt.image.BufferStrategy; 
import java.util.LinkedList; 

import ca.patrick.main.framework.GameObject; 
import ca.patrick.main.framework.Id; 
import ca.patrick.main.objects.Enemy1; 

public class Game extends Canvas implements Runnable{ 

    private static final long serialVersionUID = -4319386659730073928L; 

    public int WIDTH, HEIGHT; 
    public boolean running = false; 
    private Thread thread; 

    private LinkedList<GameObject> object; 
    private Enemy1 e; 

    private void init() { 
     HEIGHT = getHeight(); 
     WIDTH = getWidth(); 

     e = new Enemy1(200, 200, Id.Enemy); 
    } 
    public synchronized void start(){ 
     if(running) 
      return; 

     running = true; 
     thread = new Thread(this); 
     thread.start(); 
    } 

    public void run() { 
     init(); 
     this.requestFocus(); 
     long lastTime = System.nanoTime(); 
     double amountOfTicks = 60.0; 
     double ns = 1000000000/amountOfTicks; 
     double delta = 0; 
     long timer = System.currentTimeMillis(); 
     int updates = 0; 
    int frames = 0; 
    while (running) { 
     long now = System.nanoTime(); 
     delta += (now - lastTime)/ns; 
     lastTime = now; 
     while (delta >= 1) { 
      tick(); 
      updates++; 
      delta--; 
     } 
     render(); 
     frames++; 

     if (System.currentTimeMillis() - timer > 1000) { 
      timer += 1000; 
      System.out.println("FPS: " + frames + " TICKS: " + updates); 
      frames = 0; 
      updates = 0; 
     } 
    } 
} 



private void tick() { 
    e.tick(object); 
} 

private void render() { 
    BufferStrategy bs = this.getBufferStrategy(); 
    if(bs == null){ 
     this.createBufferStrategy(3); 
     return; 
    } 

    Graphics g = bs.getDrawGraphics(); 

    g.setColor(Color.cyan); 
    g.fillRect(0, 0, getHeight() * 2, getWidth() * 2); 

    e.render(g); 

    bs.show(); 
    g.dispose(); 
} 

public Game(){ 

} 

public static void main(String args[]){ 
    new Window(600, 800, "Log Platformer prototype", new Game()); 
} 

}

それを考え出しました
関連する問題