Im與Libgdx提供的AssetManager存在一些問題。 我得到一個空指針:通過AssetManager管理和加載資產(Libgdx)
Java.lang.IllegalArgumentException:紋理不能爲null。
at com.badlogic.gdx.graphics.g2d.TextureRegion.<init>(TextureRegion.java)
at com.test.test.screens.screens.MainScreen.show(MainScreen.java)
at com.badlogic.gdx.Game.setScreen(Game.java)
at com.test.test.screens.screens.SplashScreen.render(SplashScreen.java)
從來就檢查和文件it's負荷存在,並且正確的,所以it's在我的代碼的東西。我實際上不知道該怎麼做。我被告知要確保我沒有創建一個新的資產實例,而是創建一個現有的實例。不知道是否從來就做到了,雖然正確.. 這是類它的自我:
public class Assets {
public final AssetManager manager = new AssetManager();
private ObjectMap<String, Texture> textures;
private ObjectMap<String, Sound> sounds;
public final String background = "test.jpg";
public Assets() {
textures = new ObjectMap<String, Texture>();
sounds = new ObjectMap<String, Sound>();
manager.load(background, Texture.class);
}
public boolean update() {
boolean done = manager.update();
if (done) {
finishLoading();
}
return done;
}
private void finishLoading() {
textures.put(background, manager.get(background, Texture.class));
}
public Texture getTexture(String name) {
return textures.get(name);
}
public void dispose() {
manager.clear();
}
}
而此刻我宣佈它像這樣在我MainClass:
public class MainClass extends Game {
public SpriteBatch batch;
public purchaseInterface pi;
//Calls the Assets to be implemented in other classes
public Assets assets;
public MainClass(purchaseInterface purchase, GalleryOpener opener){
this.gallery= opener;
this.pi = purchase;
}
@Override
public void create() {
batch = new SpriteBatch();
assets = new Assets();
setScreen(new SplashScreen(this));
}
@Override
public void resize(int width, int height) {
super.resize(width, height);
}
@Override
public void render() {
super.render();
}
@Override
public void dispose() {
super.dispose();
batch.dispose();
assets.dispose();
}
public Assets getAssets() {
return assets;
}
@Override
public void pause() {
super.pause();
}
@Override
public void resume() {
// TODO Auto-generated method stub
super.resume();
}
} 而用於裝載資產到屏幕類例如:
public Assets assets;
public MainScreen(MainClass gam) {
game = gam;
assets = game.getAssets();
loadStore();
camera = new OrthographicCamera(screenWidth,screenHeight);
view = new StretchViewport(screenWidth, screenHeight, camera);
view.apply();
camera.translate(camera.viewportWidth/2, camera.viewportHeight/2);
}
public void loadStore() {
background = assets.getTexture(assets.background);
}
@Override
public void render(float delta) {
camera.update();
game.batch.setProjectionMatrix(camera.combined);
game.batch.begin();
game.batch.draw(background, 0, 0, 1000, 2000);
game.batch.end();
}
@Override
public void resize(int width, int height) {
view.update(width, height, true);
}
@Override
public void show() {
}
@Override
public void hide() {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void resume() {
}
@Override
public void dispose() {
background.dispose();
}
}