3

开始制作游戏。这是我的一些代码。

package games.tribe.screens;

import games.tribe.model.World;
import games.tribe.view.WorldRenderer;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;

public class GameScreen implements Screen {

private World world;
private WorldRenderer renderer;

/** This was the bit I'd missed --------------------------------------**/
@Override
public void show() {
    world = new World();
    renderer = new WorldRenderer(world);
}
/**------------------------------------------------------------------**/

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    renderer.render();
}

@Override
public void resize(int width, int height) {
    // TODO Auto-generated method stub


}

@Override
public void hide() {
    // TODO Auto-generated method stub

}

@Override
public void pause() {
    // TODO Auto-generated method stub

}

@Override
public void resume() {
    // TODO Auto-generated method stub

}

@Override
public void dispose() {
    // TODO Auto-generated method stub

}

}

这是 WorldRenderer 类:

package games.tribe.view;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;

import games.tribe.model.Block;
import games.tribe.model.TribalHunter;
import games.tribe.model.World;

public class WorldRenderer {

private World world;
private OrthographicCamera cam;

/**for debug rendering**/
ShapeRenderer debugRenderer = new ShapeRenderer();

public WorldRenderer(World world) {
    this.world = world;
    this.cam = new OrthographicCamera(10, 7);
    this.cam.position.set(5, 3.5f, 0);
    this.cam.update();
}

public void render()  {
    //render blocks
    debugRenderer.setProjectionMatrix(cam.combined);
    debugRenderer.begin(ShapeType.Rectangle);
    for(Block block : world.getBlocks()) {
        Rectangle rect = block.getBounds();
        float x1 = block.getPosition().x + rect.x;
        float y1 = block.getPosition().y + rect.y;
        debugRenderer.setColor(new Color(1, 0, 0, 1));
        debugRenderer.rect(x1, y1, rect.width, rect.height);
    }
    //render hunter
    TribalHunter hunter = world.getHunter();
    Rectangle rect = hunter.getBounds();
    float x1 = hunter.getPosition().x + rect.x;
    float y1 = hunter.getPosition().y + rect.y;
    debugRenderer.setColor(new Color(0, 1, 0, 1));
    debugRenderer.rect(x1, y1, rect.width, rect.height);
    debugRenderer.end();

}
}

这是我将其作为桌面应用程序运行时遇到的异常:

Exception in thread "LWJGL Application" java.lang.NullPointerException
at games.tribe.screens.GameScreen.render(GameScreen.java:19)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:202)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:131)
AL lib: ReleaseALC: 1 device not closed

gdx.Game.render 的第 46 行是这个方法:

@Override
public void render () {
    if (screen != null) screen.render(Gdx.graphics.getDeltaTime());
}

任何帮助将不胜感激

提前致谢

4

1 回答 1

4

GameScreen'srender()方法中,有没有renderer被初始化?如果没有,这可能会导致问题。

编辑:根据错误的前两行,您遇到的问题是NullPointerExceptionon line 19of the class GameScreen。仅当NullPointerException对象本身用于某些操作时才会发生,null因为它可能尚未初始化。

的线19GameScreen

renderer.render();

...但是该对象renderer尚未在任何地方初始化,因此目前null是默认值。为避免出现此错误,您需要renderer在运行该行代码之前初始化对象。也许是这样的:

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    renderer = new WorldRenderer();
    renderer.render();
}

我不熟悉 libgdx,所以我不能确定这就是 aWorldRenderer的初始化方式,但你需要做一些类似的事情。我希望这有帮助。

于 2012-06-25T21:03:40.790 回答