我在使用 assetManager 加载我的 Assets 时遇到了一个奇怪的问题。我的 assetManager 是我创建的一个单独的类,目的是使一切更短、更整洁。我的 Assets 类别不是静态的。我将所有 Assets 加载到非静态 assetDescriptor 中。
我加载我的启动画面,它在显示时加载我的所有 Assets 。当我调用另一个屏幕时,我加载的 Assets 无法加载并导致我的游戏崩溃。初始屏幕确实会加载,但在分配新屏幕时会崩溃。
我在这个问题上花了 2 天时间。尝试超过 15 种变化。不占上风。
我的错误信息是:
com.badlogic.gdx.utils.GdxRuntimeException: Asset not loaded: stuff.png
at com.badlogic.gdx.assets.AssetManager.get(AssetManager.java:144)
at com.badlogic.gdx.assets.AssetManager.get(AssetManager.java:167)
at com.nectar.pewdybird.mainMenu.<init>(mainMenu.java:71)
at com.nectar.pewdybird.pewdyBird.render(pewdyBird.java:68)
at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:459)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1523)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
Splash.class:
public Assets assets;
public void create(){
assets = new Assets();
assets.load();
}
public void render(){
if(assets.update()) {
setScreen(new mainMenu(this));
dispose();
} else {
//Splash Screen
gl.glClearColor(0,0,0,1);
gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
//Draws SplashScreen
batch.end();
}
}
public void dispose(){
this.screen.dispose();
this.batch.dispose();
}
mainMenu.class
public Assets assets;
private Texture Stuff;
public mainMenu(Splash game){
assets = game.assets;
Stuff = game.assets.manager.get(game.assets.stuff);
}
Assets 类
public AssetManager manager = new AssetManager(new InternalFileHandleResolver());
public final AssetDescriptor<Texture> stuff =
new AssetDescriptor<Texture>("stuff.png", Texture.class);
public void load(){
manager.load(stuff);
//12 More loads
manager.finishLoading();
}
public boolean update(){
return manager.update();
}
感谢您阅读并可能帮助解决我可能很简单的问题。
最佳答案
查看您的 mainMenu
类。您正在创建一个新的 Assets
对象,它没有任何 Assets ,因此不要创建新的 Assets
对象,而是使用 Splash
的 Assets 对象因为您在该对象上加载了 Assets 。
public Assets assets;
public Texture Stuff;
public mainMenu(){
assets = new Assets(); // why are you creating new Assets here
Stuff = assets.manager.get(assets.stuff);
}
实际上,您正在使用 Splash
类中的参数化构造函数创建 mainMenu
的对象
public mainMenu(Splash splash){
assets = splash.assets; // instead of creating new take reference of Splash class assets
Stuff = assets.manager.get(assets.stuff);
}
关于java - Android Libgdx Assets 管理器 : asset not loaded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43398726/