2014-01-13 35 views
1

我想至少使用9個圖像&他們將通過池使用。但是我只能使用一個紋理池類&不能使用其他的其他。在一個通用池中使用不同的精靈紋理AndEngine

我的代碼:我愛:

public class BubblePool extends GenericPool<Bubble> { 

public static BubblePool instance; 
private PixelPerfectTiledTextureRegion aITiledTextureRegion; 

public BubblePool(PixelPerfectTiledTextureRegion aTextureRegion) { 
    if (aTextureRegion == null) { 
     throw new IllegalArgumentException(
       "The Texture Region must not be null"); 
    } 
    this.aITiledTextureRegion = aTextureRegion.deepCopy(); 
    instance = this; 
} 

public static BubblePool sharedBubblePool() { 
    // if (instance == null) { 
    // instance = new BubblePool(); 
    // } 
    return instance; 
} 

protected void onHandleRecycleItem(final Bubble b) { 
    b.clearEntityModifiers(); 
    b.clearUpdateHandlers(); 
    b.setVisible(false); 
    b.detachSelf(); 
    Log.v("****Bubble*****", " Recycled "); 
} 

@Override 
protected synchronized void onHandleObtainItem(final Bubble b) { 

    b.reset(); 
    // b.animate(new long[] { 110, 110, 110 }, 0, 2, true); 
    // e.init();// starting modifiers 
    b.setVisible(true); 
    b.setIgnoreUpdate(false); 

} 

@Override 
protected Bubble onAllocatePoolItem() { 

    return new Bubble(0, 0, aITiledTextureRegion, 
      ResourcesManager.getInstance().vbom); 
} 

}

我最初&循環創建30個相同的精靈在場景更快的使用。

public void initiateBubble(
     final PixelPerfectTiledTextureRegion aITiledTextureRegion) { 

    bubbleList = new LinkedList<Bubble>(); 
    bubblePoolObj = new BubblePool(aITiledTextureRegion); 


    ArrayList<Bubble> bubbles = new ArrayList<Bubble>(); 
    for (int i = 0; i < 30; i++) { 
     Bubble ee = bubblePoolObj.obtainPoolItem(); 
     bubbles.add(ee); 
    } 
    for (Bubble easyEnemy : bubbles) { 
     bubblePoolObj.recyclePoolItem(easyEnemy); 
    } 
    bubbles.clear(); 
} 

然後我打電話像

Bubble aBubble = bubblePoolObj.obtainPoolItem(); 
    if (!aBubble.hasParent()) { 
    // attachChild(aEasyEnemy); 
    // add first layer 
          getChildByIndex(FIRST_LAYER).attachChild(aBubble); 

} 

池對象我如何只通過一個單一的池中使用不同的紋理&再利用?

希望你明白我的問題。

回答

2

我這樣做過,這裏是我是如何做的[可能不是最好的方法,但它的工作原理]

我假設你也想初始化不同的紋理[在這種情況下,我會把3個,每個將有10個],當你獲得它們時,它們將是隨機的。

我還假設你將爲不同的紋理使用相同的Bubble對象。

你需要新的int在游泳池[或者,如果你喜歡,你可以使用一個枚舉]

int textureOrder = 0; 

然後修改您的onAllocatePoolItem()這個

@Override 
    protected Bubble onAllocatePoolItem() { 
     switch(textureOrder){ 
      case 0: 
       return new Bubble(0, 0, aITiledTextureRegionA,ResourcesManager.getInstance().vbom); 
      case 1: 
       return new Bubble(0, 0, aITiledTextureRegionB,ResourcesManager.getInstance().vbom); 
      case 2: 
       return new Bubble(0, 0, aITiledTextureRegionC,ResourcesManager.getInstance().vbom); 
      default: 
       //this is in case you specified something unknown, you can log an error or something 
       return new Bubble(0, 0, aITiledTextureRegionA,ResourcesManager.getInstance().vbom); 
     } 
    } 

你會必須先準備3個紋理區域[在你的情況下,它將在池構造函數中]

現在添加一個新方法並調用它getsPoolItem(int textureOrder) 它會像這樣

public Bubble obtainPoolItem(int textureOrder){ 
    this.textureOrder = textureOrder; 
    return this.obtainPoolItem(); 
} 

這種方法基本上是將其紋理你想創建IF且僅當該池是空的,如果池不是空所提供的INT不會有任何效果。

現在在你的initiateBubble()方法中,有3個for循環,每個總共有10個循環,用3個不同的數字調用我們的新的obtainPoolItem()[或者有嵌套循環]來創建30個氣泡,每個類型10個。

你可能需要調用shufflePoolItems(),所以你得到一個更好的隨機元素

現在你可以繼續使用以前的代碼來獲取池項目,或者如果你想成爲政治正確的您可能需要創建池中一個名爲obtainPoolItemRandom()的新方法,只需調用[並返回] acquirePoolItem(int),並使用範圍內的隨機int,如果池已耗盡,仍然會生成隨機紋理,如果您沒有這樣做你只會從最後一個紋理生成

我希望這是有道理的,如果你需要更多的澄清留下評論,我會改善答案