我正在尝试使用4种不同颜色的方块渲染一个简单的纹理。我的语法没有错误。
例外:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.bracco.thrive.ThriveGame.create(ThriveGame.java:21)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:132)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:112)
代码
package com.bracco.thrive;
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.badlogic.gdx.Game;
import com.bracco.thrive.world.WorldGeneration;
public class ThriveGame extends Game {
WorldGeneration wGenerate;
@Override
public void create() {
wGenerate.createWorld();
}
@Override
public void dispose() {
}
@Override
public void render() {
wGenerate.render();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
}
package com.bracco.thrive.world;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
public class WorldGeneration {
private SpriteBatch spriteBatch;
private int textureWidth,textureHeight;
private boolean debug = false;
private Texture texture;
public void createWorld(){
spriteBatch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("data/textures/basictextures.png"));
}
public void render(){
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
spriteBatch.begin();
drawTexture();
spriteBatch.end();
}
private void drawTexture()
{
spriteBatch.draw(texture, 0, 16, 0, 16, 16, 16, 1, 1);
}
}
答案 0 :(得分:1)
没有制作WorldGeneration类的对象。
WorldGeneration wGenerate;
@Override
public void create() {
wGenerate=new WorldGeneration();
wGenerate.createWorld();
}