我有一个相当简单的问题,让我把头撞到我的桌子上。我的项目正在使用LibGdx框架,它试图加载一些资产。由于某种原因,它没有在正确的文件夹中看到资产。
这是我的资产管理员课程
public class LevelOneAssets {
public static final String EARTH_TEXTURE = "earth.png";
public static final String MARS_TEXTURE = "mars.png";
private static AssetManager am;
public static Class<Texture> TEXTURE = Texture.class;
public static AssetManager load(){
am = new AssetManager(new InternalFileHandleResolver());
am.load(EARTH_TEXTURE, TEXTURE);
am.load(MARS_TEXTURE, TEXTURE);
am.update();
return am;
}}
private void init(){
Gdx.app.log("GameScreen", "Initializing");
isInitialized = true;
am = LevelOneAssets.load();
world = new World(new Vector2(0f, -9.8f), true);
//Add Texture Component
engine = new PooledEngine();
RenderingSystem renderingSystem = new RenderingSystem(batch);
engine.addSystem(new AnimationSystem());
engine.addSystem(renderingSystem);
engine.addSystem(new PhysicsSystem(world));
engine.addSystem(new PhysicsDebugSystem(world, renderingSystem.getCamera()));
engine.addSystem(new UselessStateSwapSystem());
am.finishLoading();
Entity e = buildEarth(world); // error here
engine.addEntity(e);
e = buildMars(world);
engine.addEntity(e);
engine.addEntity(buildFloorEntity(world));
isInitialized = true;
}
所有资产都在LauchOff/android/assets
目录中。有任何想法吗?我正在使用Intellij和gdxVersion = '1.9.6
。
编辑----
我更新了代码,但我仍然得到Couldn't load dependencies of asset: earth.png
。我觉得这是我在Idea中设置的问题,因为我可以使用gradle运行代码。这是我的想法设置的屏幕截图。 可能出现的问题并不多......
答案 0 :(得分:2)
答案 1 :(得分:1)
这不是使用AssetManager的正确方法。你有两个选择。
在游戏线程上同步加载所有内容,等待它完成。排队等待所有内容后,请在返回前致电assetManager.finishLoading()
完全加载所有内容。
异步加载。这允许您渲染其他内容并在后台线程上加载纹理时继续动画。在对所有内容进行排队后,通过将更新调用放在呈现循环中来连续调用assetManager.update()
。当一切都已完成加载时,此方法会返回true
,并且可以安全地开始获取对已加载资源的引用。
偏离主题,但我建议不要使用对AssetManager的静态引用,因为它很容易出现内存泄漏和黑色纹理。如果您这样做,请确保您的游戏dispose()
方法在资产管理器上调用dispose()
。
答案 2 :(得分:1)
为什么使用ExternalFileHandleResolver
,您的资源位于资源文件夹中,因此请使用InternalFileHandleResolver
代替ExternalFileHandleResolver
。当资源是外部资源时(例如存储在用户手机中的图片),使用ExternalFileHandleResolver
。
public static AssetManager load(){
am = new AssetManager(new InternalFileHandleResolver());
am.load(EARTH_TEXTURE, TEXTURE);
am.load(MARS_TEXTURE, TEXTURE);
am.finishLoading(); //Load everything synchronously otherwise make continuous call of update() method
return am;
}