在我的播放屏幕中有很多纹理Texture
,字体FreeType
,图像scene2d.Image
和按钮scene2d.Button
。所以,当我设置播放屏幕时
((Game) Gdx.app.getApplicationListener()).setScreen(new PlayScreen());
打开屏幕需要几秒钟。
我想快速打开它,实现它的最佳方法是什么?
我是否创建一个新类来创建所有 Assets 然后将它们加载到屏幕上?
最佳答案
You should use the AssetManager
.管理器允许您在开始游戏时加载您的 Assets 。它还具有显示加载 Assets 百分比的功能,因此您可以显示进度条。有很多资源可以向您展示如何使用 AssetManager
。有些向您展示了带有静态 AssetManager
的示例,但 badlogic 不鼓励这样做,并可能导致黑色图像。
您应该创建一个类来存储您想要与管理器一起处理的 Assets ,并且管理器将异步加载它。我喜欢使用 AssetDescriptor
并使用静态描述符来查找我的纹理。但是您可以通过多种不同的方式使用 AssetManager
。你可以像这样向管理器添加一些东西 manager.load("images/sometexture.png, Texture.class);
然后通过 manager.get("images/sometexture.png");
这样就可以通过路径简单地查找纹理。
public class Assets {
public AssetManager manager = new AssetManager();
public static final AssetDescriptor<Texture> someTexture =
new AssetDescriptor<Texture>("images/sometexture.png", Texture.class);
public static final AssetDescriptor<TextureAtlas> uiAtlas =
new AssetDescriptor<TextureAtlas>("ui/uiskin.pack", TextureAtlas.class);
public static final AssetDescriptor<Skin> uiSkin =
new AssetDescriptor<Skin>("ui/uiskin.json", Skin.class,
new SkinLoader.SkinParameter("ui/uiskin.pack"));
public void load()
{
manager.load(someTexture);
manager.load(uiAtlas);
manager.load(uiSkin);
}
public void dispose()
{
manager.dispose();
}
}
在入口点,我创建了一个带有 Logo 的初始屏幕,然后我开始加载我的 Assets 。以下是加载 Assets 的方式。
public class MyGame extends Game
{
public void create()
{
Assets assets = new Assets();
assets.manager.load(); //starts loading assets
//option 1
assets.manager.finishLoading(); //Continues when done loading.
//it won't continue until all assets are finished loading.
}
//option 2
public void update()
{
//draw something nice to look at
if (manager.update())
{
//Assets have finished loading, change screen maybe?
setScreen(new MenuScreen(assets)); //your screen should take a Assets object in it's constructor.
}
}
}
也许您可以像往常一样加载您的菜单 Assets ,并让 Assets 管理器在用户位于菜单中时工作。那取决于你。这就是您加载 Assets 的方式。
someTexture = assets.manager.get(Assets.someTexture);
atlas = assets.manager.get(Assets.uiAtlas);
skin = assets.manager.get(Assets.uiSkin);
相同的规则适用于在 map 集中查找区域,最好是对 map 集中的每个区域执行一次。您必须将 Assets
对象传递到需要加载 Assets 的任何地方。将其设为 public static 很诱人,但它可能会导致问题。
关于java - libgdx:加载游戏中所有 Assets 的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33651323/