加载资产 LibGDX HTML (GWT) 的良好实践



由于GWT HTML5部署,我正在创建一个主要在浏览器上运行的游戏。我在图像和声音之间有相当多的 MB 资产。显然,我并不总是需要它们。我想根据接下来的屏幕和其他参数加载它们。这一点特别重要,因为我的主要部署将在网络上(启动时无法加载所有 50MB,以防万一它们将被使用)。

我真的不知道资产加载在HTML5上的LibGDX上是如何工作的。我想使用AssetManager或类似的东西来加载我的TextureAtlases和Sounds,但我认为HTML5部署只是在开始时加载所有内容,无论我以后是否在核心项目中使用AssetManager。

什么是使用 libGDX 和 GWT 的正确资产加载管道。从核心项目加载所有内容会很棒,因为这种方式对于未来将是多平台的,但现在并不重要,所以重要的部分是在网络上明智地加载资产。

提前谢谢。

我推荐的管理资产方式:

// load texture atlas
final String spriteSheet = "images/spritesheet.pack";
assetManager.load(spriteSheet, TextureAtlas.class);
// blocks until all assets are loaded
assetManager.finishedLoading();
// all assets are loaded, we can now create our TextureAtlas object
TextureAtlas atlas = assetManager.get(spriteSheet);
// (optional) enable texture filtering for pixel smoothing
for (Texture t: atlas.getTextures())
    t.setFilter(TextureFilter.Linear, TextureFilter.Linear);

现在要加载精灵,您需要执行以下操作:

// Create AtlasRegion instance according the given <atlasRegionName>
final String atlasRegionName = "regionName";
AtlasRegion atlasRegion = atlas.findRegion(atlasRegionName);
// adjust your sprite position and dimensions here
final float xPos = 0;
final float yPos = 0;
final float w = asset.getWidth();
final float h = asset.getHeight();
// create sprite from given <atlasRegion>, with given dimensions <w> and <h>
// on the position of the given coordinates <xPos> and <yPos>
Sprite spr = new Sprite(atlasRegion, w, h, xPos, yPos);

最新更新