2014-02-23 3 views
0

일부 eBook에서 LibGdx로 게임을 만드는 방법에 대한 자습서를 따르고 있습니다. 튜토리얼에는 "Canyon Bunny"라는 게임을 만드는 단계가 있습니다. 그것의 간단한 2D 게임. 하지만이 성가신 오류가 계속! (나는 또한 동일한 장르의 다른 튜토리얼에서 오류를 얻곤했다)스레드 "LWJGL Application"의 예외 java.lang.NullPointerException

나는이 게임의 개발 초기 단계에있다. 그리고 나는 약간의 테스트를하고있다. (나는이 튜토리얼의 편지를 따른다.) 나는 MAC를 사용하고 나는 전혀 운이없는 많은 해결책을 시도했다.

Exception in thread "LWJGL Application" java.lang.NullPointerException 
at com.Adel.CanyonBunny.game.WorldUpdater.updateTestObjects(WorldUpdater.java:83) 
at com.Adel.CanyonBunny.game.WorldUpdater.update(WorldUpdater.java:76) 
at com.Adel.CanyonBunny.CanyonBunnyMain.render(CanyonBunnyMain.java:39) 
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:207) 
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114) 

진정으로 노력하는 프로그래머가 직면 할 수있는 가장 실망스러운 것 중 하나입니다. 병이는 일반 프로그램에 CanyonBunnyMain되어

어떻게 든 관련이 경우 모든 클래스의 코드 ... 얻을 :

package com.Adel.CanyonBunny; 
import com.badlogic.gdx.ApplicationListener; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.graphics.GL10; 
import com.badlogic.gdx.graphics.OrthographicCamera; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.Texture.TextureFilter; 
import com.badlogic.gdx.graphics.g2d.Sprite; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
import com.badlogic.gdx.graphics.g2d.TextureRegion; 
import com.Adel.CanyonBunny.game.*; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.graphics.GL10; 
import com.badlogic.gdx.Application; 

public class CanyonBunnyMain implements ApplicationListener { 
    private static final String TAG = CanyonBunnyMain.class.getName(); 
    private WorldUpdater worldUpdater; 
    private WorldRenderer worldRenderer ; 
    private boolean paused ; 
    public void create() { 
     //I'll set the log to debug for the developing process 
     Gdx.app.setLogLevel(Application.LOG_DEBUG) ; 
     worldUpdater = new WorldUpdater(); 
     worldRenderer = new WorldRenderer() ; 

     // since, upon creation, the game is not paused, then: 
     paused = false ; 
    } 


    public void render() { 

     if (paused = true) { 
     //update the game by the time passed since the last update 
     worldUpdater.update(Gdx.graphics.getDeltaTime()) ; 
     } 
     //sets the screen color to: CornFlower Blue 
     Gdx.gl.glClearColor(0x64/255.0f, 0x95/255.0f, 0xed/255.0f, 0xff/255.0f); 
     //clears the screen to prevent flickering 
     Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT) ; 

     //Render the game to the screen 
     worldRenderer.render(); 
    } 


    public void resize (int w, int h) { 
     worldRenderer.resize(w, h) ; 
    } 
    public void pause() { 
     paused = true ; 
    } 
    public void resume() { 
     paused = false ; 
    } 

    public void dispose() { 
     worldRenderer.dispose() ; 
    } } 

이가 (너무 일반적 프로그램)을 WorldRenderer입니다 :

package com.Adel.CanyonBunny.game; 

import com.badlogic.gdx.graphics.OrthographicCamera; 
import com.badlogic.gdx.graphics.Pixmap; 
import com.badlogic.gdx.graphics.Pixmap.Format; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.g2d.Sprite; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
import com.badlogic.gdx.math.MathUtils; 

public class WorldRenderer { 
    private OrthographicCamera cam; 
    private SpriteBatch batch ; 
    private WorldUpdater updater; 

    public void WorldRenderer(WorldUpdater worldUpdater) { } 

    public void init() { } 


    public void render() { } 

    public void resize(int w, int h) { } 


    public void dispose() { } 
} 

이 (데스크톱 프로젝트 : 내 MAC에서 실행되는 하나) : 메인 클래스입니다

package com.Adel.CanyonBunny; 

import com.badlogic.gdx.backends.lwjgl.LwjglApplication; 
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration; 

public class Main { 
public static void main(String[] args) { 
    LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration(); 
    cfg.title = "CanyonBunny"; 
    cfg.useGL20 = false; 
    cfg.width = 800; 
    cfg.height = 480; 

    new LwjglApplication(new CanyonBunnyMain(), cfg); 
} 
} 

도움이 될 것입니다. 이 물었다 사람들을위한 WorldUpdater 클래스입니다 내가 당신이 여분의 데이터

을 필요로한다 말 : 또한

package com.Adel.CanyonBunny.game; 

import com.badlogic.gdx.graphics.Pixmap; 
import com.badlogic.gdx.graphics.Pixmap.Format; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.g2d.Sprite; 
import com.badlogic.gdx.math.MathUtils; 

public class WorldUpdater { 
    private final String TAG = WorldUpdater.class.getName(); 
    public Sprite[] testSprites; 
    public int selectedSprite; 
    public WorldRenderer worldRenderer; 

    public void worldUpdater() { 
     init() ; 
    } 

    public void init() { 
     initTestObjects() ; 
    } 

    private void initTestObjects() { 
     // create new array of 5 sprites 
     testSprites = new Sprite[5] ; 
     // Create empty POT-sized Pixmap with 8 bit RGBA pixel data 
     int w = 32; 
     int h = 32; 

     Pixmap pixmap = createProceduralPixmap(w, h) ; 

     //create a new texture from Pixmap data 
     Texture texture = new Texture(pixmap) ; 

     //create sprites using the just created texture 
     for (int i = 0; i < testSprites.length; i++) { 
      Sprite spr = new Sprite(texture) ; 
      spr.setSize(1,1) ; 

      //set origin to sprite's center 
      spr.setOrigin(spr.getWidth()/2.0f, spr.getHeight()/2.0f) ; 

      float randomX = MathUtils.random(-2.0f, 2.0f) ; 
      float randomY = MathUtils.random(-2.0f, 2.0f) ; 

      spr.setPosition(randomX, randomY) ; 

      //put new sprite into array 
      testSprites[i] = spr ; 
     } 
     //set first sprite as the selected one 
     selectedSprite = 0 ; 
    } 

    private Pixmap createProceduralPixmap(int width, int height) { 
     Pixmap pixmap = new Pixmap(width, height , Format.RGBA8888) ; 
     //fill the square with red color at 50% opacity 
     pixmap.setColor(1, 0, 0, 0.5f) ; 
     pixmap.fill() ; 

     //draw a yellow X in the pixmap 
     pixmap.setColor(1, 1, 0 , 1) ; 
     pixmap.drawLine(0, 0, width, height) ; 
     pixmap.drawLine(width, 0, 0, height); 

     //draw a cyan-colored border around the square 
     pixmap.setColor(0, 1, 1, 1) ; 
     pixmap.drawRectangle(0, 0, width, height) ; 

     return pixmap; 
    } 



    public void update(float deltaTime) { 
     updateTestObjects(deltaTime); 

    } 

    private void updateTestObjects(float deltaTime) { 
     //get current rotation from the selected sprite 
     float rotation = testSprites[selectedSprite].getRotation(); 

     //rotate sprite by 90 degrees per second 
     rotation += 90 * deltaTime; 

     //wrap around at 360 degrees 
     rotation %= 360 ; 

     testSprites[selectedSprite].setRotation(rotation); 
    } 


} 

을, 나는 디버깅 모드에서이 줄을 검사 할 때 :

testSprites = new Sprite[5] ; 

"testSprites을 "null을 계속 표시합니다. 나는 이것이 약간의 세부 사항을 깨끗이하기를 바란다! 덕분에 다시 . 당신의 WorldRenderer에서

+0

"CanyonBunnyMain"클래스가 포함 된 "WorldUpdater"클래스를 두 번 포함하지 않으 셨습니다. 또한이 문제는 매우 쉽게 해결할 수있는 스택 추적을 읽는 방법을 배웁니다. –

+1

문제가있는 곳인'WorldUpdater.java : 83' 줄을보십시오! 이 클래스의 코드를 게시한다면 그 점을 도와 드릴 수 있습니다. – donfuxx

답변

0

이 지정

public void WorldRenderer(WorldUpdater worldUpdater) { } 

을 그리고 WorldRendere 또한 worldUpdater의 인스턴스를 전달?

private WorldUpdater updater; 

그러나 주 파일에서 렌더러와 업데이트 프로그램의 인스턴스를 모두 만드시겠습니까?

 worldUpdater = new WorldUpdater(); 
    worldRenderer = new WorldRenderer() ; 

나는 잘 모르겠다. 눈이 피곤했거나 너무 어려울 수도 있지만 너무 복잡해 보입니다. WorldUpdater의 잘못된 인스턴스를 참조 할 수 있습니까? 머리를 감싸 주면 편집 할 수 있습니다.

+0

감사합니다. 그러나 그것은 문제가 아닙니다. 나는 노력했다. – Brainiac

1

문제는 (렌더러가 아무것도하지 않는 것처럼) 주로 업데이터에, 당신의 "생성자"함께 :

public void worldUpdater() { ... } 

생성자는 반환 유형을 지정하지 않아야합니다 - 즉, 컴파일러는 생성자로 인식 방법의 일부입니다. 코드 에서처럼 기존 객체 인스턴스에서 호출 할 수있는 방법 일뿐입니다.과 같이 변경 :

public WorldUpdater() { ... } 

참고 반환 유형의 부족과 대문자 W.

당신은 렌더러 같은 방법으로 변경할 수 있습니다. (그러나 이 기본 클래스의 해당 생성자에 업데이터를 전달해야합니다.)

또한 Nine Magics는 렌더러와 업데이터 참조를 서로 저장하는 방식이 적절하지 않다는 점을 잘 알고 있습니다. 이 문제와 관련이없는 경우에도 마찬가지입니다. 업데이트 기 클래스가 렌더러에 대해 알아야 할 이유가 없습니다. 해당 필드를 삭제해야합니다.

관련 문제