我对Libgdx提供的AssetManager存在一些问题。 我得到一个nullpointer:
Java.lang.IllegalArgumentException:texture不能为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)
我已经检查过并且加载的文件存在并且正确,所以这是我的代码中的内容。我根本不知道该怎么做..我被告知要确保我不创建一个新的Assets实例,而是创建它的现有实例。不确定我是否已正确完成.. 这是它自己的课程:
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();
}
} 以及将资产加载到Screen类的示例:
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();
}
}
答案 0 :(得分:0)
这不会加载背景纹理:
manager.load(background, Texture.class);
您需要致电
manager.finishLoading();
在load()之后。 AssetManager.load()只是存储资产的路径。 AssetManager.update()从另一个线程中的存储路径加载下一个项目,AssetManager.finishLoading()加载所有项目并等待加载线程完成。如果要在加载其他资源时绘制图像,则需要先加载该图像(在本例中为“背景”)。
另一件事,我认为你可以两次存储东西(纹理,声音对象图)。最佳做法是使用资产管理器通过“获取”功能获取纹理或任何资产。
我这样做了:
public class LoadingScreen extends Screen {
...
@Override
public void show() {
app.assets.load("data/textures/loading.pack", TextureAtlas.class);
app.assets.finishLoading(); // this is waits for the loading finish
app.assets.load("data/textures/menu.pack", TextureAtlas.class);
app.assets.load("data/textures/sprites.pack", TextureAtlas.class);
...
}
@Override
public void render(float delta) {
if (app.assets.update()) { // this is loads the next item in an other thread
app.loadingFinished(); // this is where you will create the other screens
}
...
}
...
“app”是一个游戏实例,“app.assets”是一个AssetManager实例。当我想要一个资产时,我这样做(但这只能在加载完成后运行!):
TextureAtlas atlas = app.assets.get("data/textures/sprites.pack", TextureAtlas.class);