2
我有这个GenericPool我用于在AndEngine中创建的游戏。AndEngine GenericPool与计时器添加池中的精灵
public class FruitPool extends GenericPool<Sprite> {
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Fields
// ===========================================================
private final TextureRegion mTextureRegion;
// ===========================================================
// Constructors
// ===========================================================
public FruitPool(final TextureRegion pFruitTextureRegion) {
this.mTextureRegion = pFruitTextureRegion;
}
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
protected Sprite onAllocatePoolItem() {
return new Sprite(0, 0, this.mTextureRegion);
}
@Override
protected void onHandleObtainItem(final Sprite pItem) {
pItem.reset();
}
@Override
protected void onHandleRecycleItem(final Sprite pItem) {
pItem.setVisible(true);
pItem.setIgnoreUpdate(true);
}
// ===========================================================
// Methods
// ===========================================================
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
正如你看到我提供一个TextureRegion池建立在这样的池精灵..
FruitPool pool1 = new FruitPool(TextureRegion);
Sprite blueBall = pool1.obtainItem();
我再回收精灵它已经熄灭使用
屏幕后pool1.recyclePoolItem(blueBall);
问题ia是我有一个计时器,每2秒就会添加一个Sprite。
问题是如果精灵还没有离开屏幕,并被回收我得到一个“雪碧已经有父”错误在我的Logcat。
有没有办法让我可以从池中拉出尽可能多的子画面,然后在每个屏幕上进行回收以便随后重新使用?
我现在的方式显然不是工作。
感谢您的帮助!
编辑:另外这里的方法,其中我添加取决于所选
private void addFace2() {
Random rand = new Random();
Random randFruit = new Random();
if(isGameRunning){
face = null;
int fruitNumber = randFruit.nextInt(6) + 1;
switch(fruitNumber) {
case 1:
//Item 1 code
face = pool1.obtainPoolItem();
this.pool1List.add(face);
break;
case 2:
face = pool2.obtainPoolItem();
this.pool2List.add(face);
break;
case 3:
face = pool3.obtainPoolItem();
this.pool3List.add(face);
break;
//etc. . .
case 4:
face = pool4.obtainPoolItem();
this.pool4List.add(face);
break;
case 5:
face = pool5.obtainPoolItem();
this.pool5List.add(face);
break;
}
this.mFaceCount++;
Log.e("Faces: ", "Face" + this.mFaceCount);
if(face !=null){
int rangeDuration = maxDuration2 - minDuration2;
int actualDuration = rand.nextInt(rangeDuration) + minDuration2;
MoveYModifier mod = new MoveYModifier(actualDuration, face.getY(), mCamera.getHeight());
face.registerEntityModifier(mod);
if(face.hasParent()){
Log.e("Face", "Face has parent");
}else{
this.mScene.attachChild(face);
thump.play();
}
}
}
听起来像一个好办法池莉构建。这听起来像来自你给的描述。我的泳池里已经有这些东西了?如果我错了,请告诉我我需要改变什么。这将会非常有帮助。谢谢。 – 2011-12-23 15:24:56
我一直在阅读我的代码,它看起来像我在你的代码中的答案张贴相同的大纲?我对么? – 2011-12-23 18:46:49
你的游泳池不是按照它的方式建造的(按照我的方法)。现在我将使用游泳池的完整源代码对其进行编辑。 – Jong 2011-12-24 08:36:44