2012-03-15 220 views
1

我似乎無法像我期望的那樣讓TiledSprite工作。沒有手冊,AndEngineExamples中的示例沒有任何幫助。TiledSprite沒有按預期工作

考慮以下代碼:

@Override 
public void onLoadResources() { 
    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/"); 
    mBlockAtlas = new BitmapTextureAtlas(512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA); 
    mBlockTexture = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mBlockAtlas, getApplicationContext(), "num_plasma.png", 0, 0, 3, 3); 

    getTextureManager().loadTexture(mBlockAtlas); 
} 

這加載一個512x512的質感,我已經 - 人工 - 分爲3x3的瓷磚只是爲了測試目的。現在,這樣做:

public Scene onLoadScene() { 
    mScene = new Scene(); 
    mScene.setBackground(new ColorBackground(0.5f, 0.1f, 0.6f)); 

    TiledSprite foo, foo2; 
    foo = new TiledSprite(0, 0, mBlockTexture); 
    foo.setCurrentTileIndex(1); 
    foo2 = new TiledSprite(0, 200, mBlockTexture); 
    foo2.setCurrentTileIndex(5); 
    mScene.attachChild(foo); 
    mScene.attachChild(foo2); 

    return mScene; 
} 

這使得兩個精靈在屏幕上(如預期),但他們都顯示相同的瓷磚(5)!

如果你有一堆使用相同紋理但顯示不同瓷磚的精靈,應該怎麼做?

Screenshot of error

回答

2

我認爲你需要deepcopy的()的時候,你所創建的精靈的紋理,像

foo2 = new TiledSprite(0, 200, mBlockTexture.deepCopy()); 
+0

謝謝,這幫助它。對我的60+精靈使用deepCopy()會確保很多內存,所以我在考慮使用TextureRegionFactory.extractFromTexture(),但是該方法需要一個Texture,而我的紋理是一個TextureRegion。你有任何線索如何使這項工作? – bos 2012-03-15 12:06:31

+1

我沒有使用TextureRegionFactory,但TextureRegion擴展了BaseTextureRegion,它具有getTexture()的getter() – jmroyalty 2012-03-15 12:27:26

+0

您確實是一個節省時間的工具。謝謝! – bos 2012-03-15 12:34:19