Android 在一个通用池和引擎中使用不同的精灵纹理

Android 在一个通用池和引擎中使用不同的精灵纹理,android,andengine,pool,Android,Andengine,Pool,我想使用至少9个图像&它们将通过池使用。但是对于一个Pool类,我只能使用一个纹理&不能使用其他纹理 我的代码:比如: public class BubblePool extends GenericPool<Bubble> { public static BubblePool instance; private PixelPerfectTiledTextureRegion aITiledTextureRegion; public BubblePool(PixelPerfectTi

我想使用至少9个图像&它们将通过池使用。但是对于一个Pool类,我只能使用一个纹理&不能使用其他纹理

我的代码:比如:

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);
}
我如何使用不同的纹理&仅通过一个池重复使用


希望你能理解我的问题。

我以前做过,下面是我是如何做到的[可能不是最好的方法,但它很有效]

我假设您还想初始化不同的纹理[在本例中,我将放置3个,每个纹理有10个],当您获得它们时,它们将是随机的

我还假设您将对不同的纹理使用相同的气泡对象

在池中需要一个新的int[或者如果愿意,可以使用枚举器]

int textureOrder = 0;
然后将您的onAllocatePoolItem()修改为

您必须提前准备3个纹理区域[在您的情况下,它将在池构造函数中]

现在添加一个新方法,并将其称为GetainPoolItem(int-textureOrder) 会是这样的

public Bubble obtainPoolItem(int textureOrder){
    this.textureOrder = textureOrder;
    return this.obtainPoolItem();
}
此方法基本上设置当且仅当池为空时要创建的纹理,如果池不为空,则提供的int将无效

现在,在initiateBubble()方法中,使用3个for循环,每个循环总共10个,以使用3个不同的数字调用新的OccessPoolItem()[或使用嵌套循环]来创建30个气泡,每种类型10个

您可能需要调用ShufflePolitems(),以便获得更好的随机元素

现在,您可以继续使用以前的代码来获取池项,或者如果您想在政治上正确,您可能需要在池中创建一个名为obtainPoolItemRandom()的新方法,该方法只调用[并返回]obtainPoolItem(int),其中包含范围内的随机int,如果池耗尽,您仍然可以生成随机纹理,如果未执行此操作,则仅从最后一种类型纹理生成

我希望这是有意义的,如果你需要更多的澄清,请留下评论,我会改进答案

int textureOrder = 0;
 @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);
        }
    }
public Bubble obtainPoolItem(int textureOrder){
    this.textureOrder = textureOrder;
    return this.obtainPoolItem();
}