开始制作游戏。 这是我的一些代码。
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());
}
任何帮助将不胜感激
提前致谢
答案 0 :(得分:4)
在GameScreen
的{{1}}方法中,render()
是否已初始化?如果没有,那可能会导致问题。
修改:根据错误的前两行,您遇到的问题是renderer
班级NullPointerException
19
}。 GameScreen
仅在对象本身为NullPointerException
时使用某个操作时才会发生,因为它可能尚未初始化。
null
的{{1}}行是:
19
...但是对象GameScreen
尚未在任何地方初始化,因此它当前是renderer.render();
,这是默认值。为避免出现此错误,您需要在运行该行代码之前初始化renderer
对象。也许是这样的:
null
我不熟悉libgdx,所以我无法确定renderer
是如何初始化的,但你需要做一些类似的事情。我希望这会有所帮助。