I want to use at least 9 image & they will be use via pool. But i can use only one texture for a Pool Class & can't use rest other.

My code: Like:

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);
}

}

I create 30 same sprites initially & recycle for faster use in Scene.

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();
}

Then I call the pool object like

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

}

How i use different texture & re-use via only one single pool?

Hope, You understand my problem.

有帮助吗?

解决方案

I've done this before , here's how I did it [might not be the best approach, but it works]

I'm assuming you also want to initialize different textures [in this case I'll put 3, each will have 10 of them] and when you obtain them they will be random.

I'm also assuming that you will be using the same Bubble object for the different textures.

you'll need a new int in the pool [or you can use an enumerator if you like]

int textureOrder = 0;

Then modify your onAllocatePoolItem() to this

 @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);
        }
    }

you'll have to prepare the 3 texture regions ahead of that [in your case, it'll be in the pool constructor]

now add a new method and call it obtainPoolItem(int textureOrder) it'll be like this

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

this method basically sets which texture you wanna create IF AND ONLY IF the pool is empty, if the pool is not empty the provided int will have no effect.

now in your initiateBubble() method, have 3 for loops each with a total of 10 to call our new obtainPoolItem() with 3 different numbers [or have nested loops] to create 30 bubbles, 10 of each type.

you may want to call shufflePoolItems() so you get a better random elements

now you can keep on using your previous code to get the pool item, or if you wanna be politically correct you may need to create a new method in the pool called obtainPoolItemRandom() that just calls [and returns] obtainPoolItem(int) with a random int from your range, in case the pool is depleted you'd still generate random textures, if you didn't do this you'll only generate from the last type texture

I hope this makes sense, if you need any more clarification leave a comment and I'll improve the answer

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top