2017-02-28 71 views
0

我到這一點時,我不得不更換所有new Sprite(texture)new Sprite(manager.get(imgPath,Texture.class))以優化精靈創建和啓動遊戲之前做的所有負載。AssetManager紋理給人黑色矩形

問題是,當我使用公共的AssetManager加載所有紋理時,我會看到黑色的矩形。

但是,每次我需要加載紋理時都使用新的AssetManager()可以正常工作(也許是因爲此管理器很快被garbadge收集,沒有時間處理它創建的紋理?),但那樣沒有意義使用它。

下setupBody()方法被調用我的精靈體包裝器構造的端部。

運行的代碼(但沒用)─爲每個精靈一個新AssetManager:

protected void setupBody(String path){ 
    BodyEditorLoader loader = new BodyEditorLoader(Gdx.files.internal(path)); 
    BodyDef bDef = new BodyDef(); 
    bDef.type = BodyDef.BodyType.DynamicBody; 
    bDef.position.set(this.position); 
    bDef.angle = this.rotation; 
    body = mWorld.createBody(bDef); 
    body.setUserData(this); 
    FixtureDef fix = new FixtureDef(); 
    fix.density = 0.1f; 
    fix.friction = 0f; 
    fix.restitution = 0.5f; 
    fix.filter.categoryBits = this.getCategory(); 
    fix.filter.maskBits = this.getMask(); 
    origin = loader.getOrigin("base"+identifier, 1).cpy(); 
    String imgPath = loader.getImagePath("base"+identifier); 

    AssetManager manager = new AssetManager(); 
    manager.load(imgPath,Texture.class); 
    manager.finishLoading(); 
    Texture baseSpriteTexture = manager.get(imgPath,Texture.class); 
    Texture baseSpriteTexture = new Texture(imgPath); 
    baseSprite = new Sprite(baseSpriteTexture); 

    loadHighlightedSprite(loader, identifier); 
    attachFixture(loader, identifier, fix); 
} 

代碼讓我無眠─這個想法是有一個單一的非靜態經理:

protected void setupBody(String path){ 
    /*no changes before */ 
    String imgPath = loader.getImagePath("base"+identifier); 

    AssetManager manager = SmallIntestineDemoGame.getAssets(); 

    manager.load(imgPath,Texture.class); 
    /* no changes after */ 
} 

SmallIntestineDemoGame $ getAssets():

public class SmallIntestineDemoGame extends Game { 

    public AssetManager assets; 



    @Override 
    public void create() { 
     setScreen(new GameScreen()); 
    } 

    public static AssetManager getAssets() { 
     return ((SmallIntestineDemoGame) Gdx.app.getApplicationListener()).assets; 
    } 

    public void setupAssets(){ 
     this.assets = new AssetManager(); 
     Texture.setAssetManager(assets); 
    } 

} 

調試:

調試我的資產管理器讓我感到困惑。經理的地圖卡在33資產值的值的大小,即使我添加更多:

我測試了這兩種情況下─用新AssetManager()的應用程序(其次是finishloading和所有需要的東西)爲每個精靈創建。並通過一個獨特的資產管理器傳遞給所有精靈創作:

我在draw方法中設置了斷點,除了sprite.texture.glhandle之外,兩個精靈幾乎沒有區別! 當使用相同的管理器創建所有精靈紋理時,它們的紋理glhandle設置爲0,這解釋了黑色矩形。但是,當使用本地新創建的資產管理器創建每個精靈時,這個精確的句柄有一些適當的值(2269)。

+0

您尚未顯示足夠的相關代碼以查看出了什麼問題。像加載所有內容的位置以及獲取紋理引用以製作精靈的位置。 – Tenfour04

+0

我還沒有加載任何東西。我只是嘗試使用一個AssetManager。目前我一個接一個加載(你會在setupBody()內看到finishLoading())。實際上,setupBody()是一切都完成的地方。 遊戲通過創建大量OrganPart實例開始。 OrganPart是精靈體包裝。它有一個box2d主體和一個精靈。所有這些都是在OrganPart構造函數結尾處調用的setupBody()處初始化的。 – neogineer

+0

如果您需要更多信息,我在這裏。 – neogineer

回答

0
Texture.setAssetManager(assets); 

當您設置AssetManager時,您需要每次在您的遊戲恢復時調用AssetManager的update()方法。

如果不設置AssetManager,平時管理質感機制會踢,所以你不必擔心什麼。

+0

你的意思做的那樣: /* GameScreen $渲染()*/ @覆蓋 公共無效渲染(浮動三角洲){ Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); SmallIntestineDemoGame.getAssets()。update(); stage.draw(); stage.act(delta); hudStage.draw(); } 也不工作。 – neogineer

+0

Texture.setAssetManager(assets);在setupAssets()方法中刪除此語句並檢查。 – Aryan

+0

仍然是黑色的矩形。有和沒有調用update()每一幀。 – neogineer