使用不同的精灵质地一个通用池AndEngine [英] Use different sprites texture in one generic pool AndEngine

查看:150
本文介绍了使用不同的精灵质地一个通用池AndEngine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用至少9图像和放大器;他们将通过池使用。但我可以只使用一个纹理池类和放大器;不能使用REST等。

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.

我的code:我爱:

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相同的精灵;回收的场景中更快地使用。

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]

我假设你也想初始化不同的纹理[在这种情况下,我会把3,每次都会有10人],当你获得他们,他们将是随机的。

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.

您将需要新的int在泳池[或者你可以使用一个枚举,如果你喜欢]。

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

int textureOrder = 0;

然后修改onAllocatePoolItem()这个

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

你必须prepare 3纹理区域领先的[在你的情况,这将是在泳池构造]

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

现在添加一个新的方法,并调用它obtainPoolItem(INT textureOrder) 它会是这个样子

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

此方法主要设置的纹理,你想创建当且仅当池是空的,如果池不是空所提供的INT不会有任何效果。

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.

现在你initiateBubble()方法,有3环,每个环共有10拨打我们的新obtainPoolItem()与3个不同的数字[或嵌套循环]创建30气泡,10每种类型的。

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.

您可能需要调用shufflePoolItems(),所以你得到一个更好的随机元素

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

现在你可以继续使用您的previous code拿到池项目,或者如果你想成为政治正确的,你可能需要创建名为obtainPoolItemRandom(池的新方法)只是调用[返回] obtainPoolItem(INT)与你的范围内的随机INT,以防池耗尽你还是会产生随机纹理,如果你不这样做,你就只能从最后一个类型的纹理生成

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

这篇关于使用不同的精灵质地一个通用池AndEngine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆