2011-12-23 64 views
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(); 
      } 

      } 
    } 

回答

3

数这可能是因为您尝试一旦你得到它(即使它赢得了”以精灵附着到现场精灵不会发生在你的代码中),并且一旦它被回收,你就不会分离它。

的方法,我建议你用的是这个(我在我的游戏,我有一个精灵池像你使用它,它工作正常):

  1. FruitPool底舱引用到场景。然后,当一个精灵是创建的(在onAllocatePoolItem)你将它附加到场景,为好。你从来没有分离它再次(除非游戏重置,当然...)
  2. 当一个精灵获得(onHandleObtainItem)你打电话reset()就可以了。它重置所有的领域,如位置,旋转等... 注意:这是的不是删除EntityModifier s!它只是重置它们。完成后应将其删除,否则会导致问题。
  3. 当一个精灵被回收(onHandleRecycleItem),你调用方法setIgnoreUpdate(true)setVisible(false),所以这个精灵不会被绘制和更新。

这样,当你得到一个项目:

Sprite sprite = pool.obtainPoolItem(); 

它附加到现场,你不调用它的任何复位或初始化方法 - 刚刚成立它是你想要的位置,并注册EntityModifier

编辑:这里是池的全部源:

public class FruitPool extends GenericPool<Sprite> { 
     // =========================================================== 
     // Constants   
     // =========================================================== 

     // ===========================================================   
     // Fields   
     // =========================================================== 
     private final TextureRegion mTextureRegion; 
     private final Scene mAttachedScene; 
     // ===========================================================   
     // Constructors   
     // =========================================================== 
     public FruitPool(final TextureRegion pFruitTextureRegion, final Scene pScene) { 
      this.mTextureRegion = pFruitTextureRegion; 
      this.mAttachedScene = pScene; 
     } 
     // ===========================================================   
     // Getter & Setter   
     // =========================================================== 

     // ===========================================================   
     // Methods for/from SuperClass/Interfaces   
     // =========================================================== 
     @Override 
     protected Sprite onAllocatePoolItem() { 
      Sprite newSprite = new Sprite(0, 0, this.mTextureRegion); 
      this.mAttachedScene.attachChild(newSprite); //Attaching it HERE to the scene. 
      return newSprite; 
     } 
     @Override 
     protected void onHandleObtainItem(final Sprite pItem) { 
      pItem.reset(); 
     } 
     @Override 
     protected void onHandleRecycleItem(final Sprite pItem) { 
      pItem.setVisible(true); 
      pItem.setIgnoreUpdate(true); //Just make it ignore updates while it is recycled, DON'T remove it from the scene. 
     } 
     // ===========================================================   
     // Methods   
     // =========================================================== 

     // ===========================================================   
     // Inner and Anonymous Classes   
     // =========================================================== 
} 
+0

听起来像一个好办法池莉构建。这听起来像来自你给的描述。我的泳池里已经有这些东西了?如果我错了,请告诉我我需要改变什么。这将会非常有帮助。谢谢。 – 2011-12-23 15:24:56

+0

我一直在阅读我的代码,它看起来像我在你的代码中的答案张贴相同的大纲?我对么? – 2011-12-23 18:46:49

+0

你的游泳池不是按照它的方式建造的(按照我的方法)。现在我将使用游泳池的完整源代码对其进行编辑。 – Jong 2011-12-24 08:36:44

相关问题