我想使用至少 9 张图像,它们将通过池使用。但是我只能为一个池类使用一个纹理,不能使用其他纹理。
我的代码:喜欢:
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 个相同的 Sprite 并回收以便在场景中更快地使用。
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);
}
我如何使用不同的纹理并仅通过一个池重复使用?
希望,你明白我的问题。
最佳答案
我以前做过,下面是我的做法[可能不是最好的方法,但它有效]
我假设您还想初始化不同的纹理 [在这种情况下我会放 3 个,每个都有 10 个],当您获得它们时它们将是随机的。
我还假设您将对不同的纹理使用相同的泡泡对象。
你需要在池中有一个新的 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 个纹理区域 [在您的情况下,它将在池构造函数中]
现在添加一个新方法并将其命名为 obtainPoolItem(int textureOrder) 会是这样
public Bubble obtainPoolItem(int textureOrder){
this.textureOrder = textureOrder;
return this.obtainPoolItem();
}
此方法基本上设置了当且仅当池为空时您要创建的纹理,如果池不为空,则提供的 int 将无效。
现在在您的 initiateBubble() 方法中,有 3 个 for 循环,每个循环总共有 10 个来调用我们新的 obtainPoolItem(),使用 3 个不同的数字 [或嵌套循环] 来创建 30 个气泡,每种类型 10 个。
你可能想调用 shufflePoolItems() 以获得更好的随机元素
现在您可以继续使用之前的代码来获取池项目,或者如果您想要在政治上正确,您可能需要在池中创建一个名为 obtainPoolItemRandom() 的新方法,它只调用 [并返回] obtainPoolItem(int ) 使用范围内的随机整数,以防池耗尽时你仍会生成随机纹理,如果你不这样做,你只会从最后一种类型的纹理生成
我希望这是有道理的,如果您需要更多说明,请发表评论,我会改进答案
关于android - 在一个通用池 AndEngine 中使用不同的 Sprite 纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21090279/