libgdx: best way to load all assets in the game(libgdx:加载游戏中所有资源的最佳方式)
问题描述
在我的播放画面中有很多纹理Texture
、字体FreeType
、图片scene2d.Image
和按钮scene2d.Button代码>.所以,当我设置播放屏幕时
In my play screen has a lot of textures Texture
, fonts FreeType
, images scene2d.Image
and buttons scene2d.Button
. So, when I set play screen
((Game) Gdx.app.getApplicationListener()).setScreen(new PlayScreen());
打开屏幕需要几秒钟.
我想快速打开它,最好的方法是什么?
我是否创建一个新类来创建所有资产然后将它们加载到屏幕上?
Do I create a new class to create all assets then load them on the screens?
推荐答案
你应该使用 AssetManager
.管理器允许您在开始游戏时加载资产.它还具有显示加载资产百分比的功能,因此您可以显示进度条.有很多资源可以向您展示如何使用 AssetManager
.有些人向您展示了带有静态 AssetManager
的示例,但是 badlogic 不鼓励这样做,并且可能导致黑色图像.
You should use the AssetManager
. The manager allows you to load your assets when you start the game. It also has the functionality to show you the percentage of assets loaded so you can show a progress bar. There are plenty of sources out there that show you how to use the AssetManager
. Some show you examples with a static AssetManager
but this is discouraged by badlogic and can cause black images.
您应该创建一个类来存储您想要与管理器处理的资产,并且管理器将异步加载它.我喜欢使用 AssetDescriptor
并使用静态描述符来查找我的纹理.但是您可以通过多种不同的方式使用 AssetManager
.您可以像 manager.load("images/sometexture.png, Texture.class);
那样向管理器添加一些东西,然后通过执行 manager.get("images/sometexture.png");
这样它可以简单地通过它的路径查找纹理.
You should make a class where you store your assets you want to handle with the manager and the manager will load it asynchronously. I like to work with the AssetDescriptor
and using static descriptors to lookup my textures. but there are many different ways you can work with the AssetManager
. You can just add a something to the manager like so manager.load("images/sometexture.png, Texture.class);
and look it up by doing manager.get("images/sometexture.png");
this way it simple looks up the textures by it's path.
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();
}
}
在入口点中,我创建了一个带有徽标的启动画面,然后开始加载我的资产.这是加载资产的方式.
In the entry point I create a splash screen with a logo then I start loading my assets. Here is how you load the 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.
}
}
}
也许您可以像往常一样加载您的菜单资产,并让资产管理器在用户在菜单中时工作.那取决于你.这就是您加载资产的方式.
Perhaps you can load your menu assets like you normally and let the asset manager work while the user is in the menu. That is up to you. This is how you load a asset.
someTexture = assets.manager.get(Assets.someTexture);
atlas = assets.manager.get(Assets.uiAtlas);
skin = assets.manager.get(Assets.uiSkin);
同样的规则适用于在您的地图集中查找区域,最好是对地图集中的每个区域执行一次.您必须将 Assets
对象传递到您需要加载资产的任何地方.将其设为 public 静态很诱人,但它可能会导致问题.
The same rules apply for looking up regions in your atlas, best is to do this a single time for each region in your atlas. You have to pass around the Assets
object around to anywhere you need to load assets. It makes it tempting to make it public static but it can cause issues.
这篇关于libgdx:加载游戏中所有资源的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:libgdx:加载游戏中所有资源的最佳方式
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 转换 ldap 日期 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 获取数字的最后一位 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01