Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6982943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:21:16+00:00 2026-05-27T18:21:16+00:00

I have this GenericPool i am using for a Game i am creating in

  • 0

I have this GenericPool i am using for a Game i am creating in AndEngine.

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          
// ===========================================================  
 }

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

           }
     }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:21:16+00:00Added an answer on May 27, 2026 at 6:21 pm

    It’s probably because you try to attach the sprite to the scene once you obtained it (Even though it won’t happen in your code..), and you didn’t detach it once it is recycled.

    The approach I suggest you to use is this (I am using it in my game where I have a sprite pool like yours, and it works fine):

    1. The FruitPools hold references to your scene. Then, when a sprite is created (In onAllocatePoolItem) you attach it to the scene, for good. You never detach it again (Unless the game resets, of course…)
    2. When a sprite is obtained (onHandleObtainItem) you call reset() on it. That resets all it’s fields, like position, rotation, etc… Note: this does not remove the EntityModifiers! It just resets them. You should remove them when they are done, otherwise it will cause problems.
    3. When a sprite is recycled (onHandleRecycleItem) you call the methods setIgnoreUpdate(true) and setVisible(false) so this sprite won’t be drawn and updated.

    This way, when you obtain an item:

    Sprite sprite = pool.obtainPoolItem();
    

    You don’t attach it to the scene, and you don’t call any reset or initialization methods on it – just set it’s position as you want, and register the EntityModifier.

    EDIT: Here is the full source for the pool:

     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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created this Pool that has 5 static variables. public class FruitPool extends
I have this class class Validator implements iValidation{ protected $_fields, $_errors; public function __construct($fields){
I have a Generic Pool that i am making here: public class FruitPool extends
I have this basic setup: enum{ BASE, PRIMITIVE, ... }; class IUnknown{ public: bool
I have this code but it doesn't work! public class Trial { public static
I have this class: public abstract class AbstractIncomingCall { /* class properties */ public
I have this class where I am using a combination of jQuery and Prototype
I am using AndEngine to create a game. I have randomly added sprites in
Have this scenario: public class Base { public string Name; } public Class ClassA
I have this string 'john smith~123 Street~Apt 4~New York~NY~12345' Using JavaScript, what is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.