AndEngine GenericPool with Timer adding sprites from the Pool

I have this GenericPool i am using for a Game i am creating in AndEngine. public class FruitPool extends GenericPool { // =========================================================== // 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 // =========================================================== } As you see i supply a TextureRegion to the pool to create a sprite in the pool like this.. FruitPool pool1 = new FruitPool(TextureRegion); Sprite blueBall = pool1.obtainItem(); I then recycle the sprites after it has gone off the screen using pool1.recyclePoolItem(blueBall); The issue i a having is that i have a Timer that adds a Sprite just about every 2 seconds. The problem is if the sprite hasnt gone off the screen yet and been recycled i get a "Sprite already has Parent" error in my Logcat. Is there a way i can make it possible to pull as many Sprites as i want out of the Pool and then recycle each one as it goes off the screen to be later reused? The way i have it right now clearly isnt working. Thanks for the help Guys! EDIT: Also here is the method where i add the sprites depending on the number selected 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(); } } }

以上就是AndEngine GenericPool with Timer adding sprites from the Pool的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏