如何使用.assets.load(「file」,file.type)加載精靈在這種情況下,精靈的文件類型應該是什麼?使用資產經理LibGDX加載精靈
1
A
回答
3
我猜,你usualy不直接加載Sprite
,但你加載它Texture
並創建一個Sprite
出來。
因此,您致電assets.load("file", Texture.class)
,然後創建一個Sprite
與您加載的Texture
:
Sprite sprite = new Sprite(asstes.get("file", Texture.class))
。
但我建議您使用TextureAtlas
而不是Texture
。
A TextureAtlas
是某種「紋理集合」,它基本上是一個大的Texture
,它本身具有所有單個的Texture
。
您可以使用assets.load("atlas", TextureAtlas.class)
加載它並使用它: TextureAtlas atlas = assets.get("atlas", TextureAtlas.class)
。
然後,您可以創建Sprite
這樣的:
Sprite sprite = atlas.createSprite("spriteName");
要創建一個TextureAtlas
你可以使用TexturePacker
。
1
不建議直接加載精靈。當Android上發生上下文丟失時,它將釋放佔用您已加載資源的內存。因此,在上下文丟失後直接訪問您的資產會立即使恢復的應用程序崩潰。
爲了防止上述問題,您應該使用AssetManager加載和存儲資源,如紋理,位圖字體,瓷磚貼圖,聲音,音樂等。通過使用AssetManager,您只需加載一次資產。
我建議這樣做的方法如下:
// 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);
相關問題
- 1. Libgdx - 正在加載資產
- 2. Android Libgdx assetManager:資產未加載
- 3. libgdx - 精靈不旋轉
- 4. 刪除精靈Libgdx Java
- 5. 刪除精靈Libgdx Java
- 6. LIBGDX資產管理器加載真實字體字體
- 7. 資產管理器加載pixmap out of mem Libgdx Java
- 8. 通過AssetManager管理和加載資產(Libgdx)
- 9. 使用AssetManager在LibGDX,Java中加載資產
- 10. 使用Async任務加載所有資產 - Libgdx
- 11. 資產在Libgdx
- 12. 使用FObjectFinder加載資產
- 13. 資產經理,獲取資產ID?
- 14. 產卵精靈gles2
- 15. 精靈和產卵
- 16. 用批處理繪製一個部分精靈 - LibGdx
- 17. 如何使用libGDX使用鍵盤按鍵移動精靈?
- 18. LibGdx Box2d collison與精靈不匹配
- 19. Libgdx java精靈大小問題
- 20. Android libgdx ...水平來回移動精靈
- 21. libgdx,水平移動精靈列表
- 22. 繪製一個單精靈Pixmap - LibGDX
- 23. 如何獲取資源加載的精靈大小?
- 24. 產生敵人精靈
- 25. 羅盤產生的精靈
- 26. 行動中產卵精靈
- 27. libGDX從精靈或紋理獲取像素顏色
- 28. 使用寶石加載資產(遇到問題不會加載資產)
- 29. LibGDX如何2個紋理單一位置使用精靈和SpriteBatch
- 30. 可執行LibGDX Jar文件不會加載資產
而不是創建一個新的'Sprite'了'AtlasRegion'的,你可以直接從'TextureAtlas'用'atlas.createSprite(創造它spritename)'。同樣'TextureFilter'可以在'TexturePacker'的配置中設置,因此它被設置爲整個'TextureAtlas'。 – Springrbua 2014-12-05 12:41:29
這個答案不完全正確。使用或不使用AssetManager時,libgdx中的紋理會自動*託管*,因此會在上下文丟失後自動重新加載。 – Tenfour04 2014-12-05 16:23:12
在libgdx中,您可以擁有託管和非託管紋理。因此,確實可以通過確保自動管理紋理來避免使用AssetManager。這是從FileHandle加載紋理時的情況,但如果它們是從Pixmap加載的,則必須在'resume()'或者'resize()'方法中手動重新加載它們。爲了避免這種頭痛,我建議使用'AssetManager'。 – Gio 2014-12-05 21:18:39