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

  • Home
  • SEARCH
  • 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 7055297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:43:53+00:00 2026-05-28T03:43:53+00:00

I have created this Pool that has 5 static variables. public class FruitPool extends

  • 0

I have created this Pool that has 5 static variables.

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

// ===========================================================          
// Fields         
// =========================================================== 
private  ITextureRegion texture1;
private  ITextureRegion texture2;
private ITextureRegion texture3;
private  ITextureRegion texture4;
private  ITextureRegion texture5;

private Scene mScene;
private Context mContext;
private Camera mCamera;
private LinkedList<Sprite>pool1;

private static Sprite fruitOne;
private static Sprite fruitTwo;
private static Sprite fruitThree;
private static Sprite fruitFour;
private static Sprite fruitFive;
private  Sprite fruit;
// ===========================================================          
// Constructors          
// =========================================================== 
public FruitPool(final ITextureRegion watermelonRegion,
        ITextureRegion cherryRegion,ITextureRegion mBallTextureRegion, ITextureRegion grapeTextureRegion, ITextureRegion strawberryTextureRegion,Scene mScene2, Camera camera, LinkedList<Sprite>items) {

    this.texture1 = watermelonRegion;
    this.texture2 =cherryRegion;
    this.texture3 = mBallTextureRegion;
    this.texture4 = grapeTextureRegion;
    this.texture5 = strawberryTextureRegion;
    this.mScene = mScene2;
    this.pool1 = items;

    this.mCamera = camera;

}
// ===========================================================          
// Getter & Setter          
// =========================================================== 

// ===========================================================          
// Methods for/from SuperClass/Interfaces          
// ===========================================================  
@Override
protected Sprite onAllocatePoolItem() {


     Random randFruit = new Random();

     int textureNumber = randFruit.nextInt(5)+1;

     switch(textureNumber){
     case 1:
         if (fruitOne == null) {
              fruitOne = new Sprite(0, 0, this.texture1);
              Log.e("FruitPool", "Item rremade");
            } else {
              fruit = fruitOne;
              Log.e("FruitPool", "Item exist in pool..Used");
            }
          break;
     case 2:
         if(fruitTwo == null){
          fruitTwo = new Sprite(0, 0, this.texture2);
         }else{
             fruit = fruitTwo;
             Log.e("FruitPool", "Item exist in pool..Used");
         }

         break;
     case 3:
         if(fruitThree == null){
              fruitThree = new Sprite(0, 0, this.texture3);
         }else{
             fruit = fruitThree;
             Log.e("FruitPool", "Item exist in pool..Used");
         }

         break;
     case 4:
         if(fruitFour == null){
             fruitFour = new Sprite(0, 0, this.texture4);
         }else{
             fruit = fruitThree;
             Log.e("FruitPool", "Item exist in pool..Used");

         }

         break;
     case 5:
         if(fruitFive == null){
              fruitFive = new Sprite(0, 0, this.texture5);
         }else{
             fruit = fruitFive;
             Log.e("FruitPool", "Item exist in pool..Used");
         }

         break;

     }


    return fruit;

}
@Override
protected void onHandleObtainItem(final Sprite pItem) {
    pItem.reset();
}
@Override
protected void onHandleRecycleItem(final Sprite pItem) {
    pItem.setVisible(false);
    pItem.setIgnoreUpdate(true);

}



// ===========================================================          
// Methods          
// ===========================================================  

// ===========================================================          
// Inner and Anonymous Classes          
// ===========================================================  
}

As you see in my onAllocate method i check to see if the item exist’s if it does i return it in the method.

So in my main activity i use

          face =  fruitsPool.onAllocatePoolItem();

This works initially but the problem is i attach the face to the scene about every second.And i get the error that the sprite has already been attached to the scene. At first the only way i found to do this was to create a new Sprite each second, and detach it when im finished with it, but this uses wayy to much memory and causes lag, na freezes.

Does anyone have any pointers for what i need to do, or suggestions for me code?

  • 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-28T03:43:54+00:00Added an answer on May 28, 2026 at 3:43 am

    You never call onAllocatePoolItem. This method is called internally in the GenericPool<T> class when the pool is empty and an item is requested.

    The only methods you should be calling from outside of your FruitPool class are:

    1. obtainPoolItem to get a pool item; Not onAllocatePoolItem.
    2. recyclePoolItem to recycle an item. Remember to call it when you are done with the item.
    3. batchAllocatePoolItems, but you don’t need it in your case. It could be used when you want to create the pool items at a certain point; But initiating Sprite‘s is not a heavy process, so you don’t need.

    You shouldn’t call any other methods.

    Again, you are breaking the purpose of the object pool! onAllocatePoolItem should always return a brand new object! Not an existing one! That’s the reason you get the

    Entity already has a parent

    error.

    By the way, when I first built this class for you, I attached the sprite to the scene when it’s created. Why aren’t you doing it now?

    I have edited it, and added some comments.

    public class FruitPool extends GenericPool<Sprite> {
    // ===========================================================
    // Constants          
    // ===========================================================
    
    // ===========================================================          
    // Fields         
    // =========================================================== 
    private ITextureRegion[] mTextureRegions = new ITextureRegion[5];
    private Scene mScene;
    private int mCount;
    // ===========================================================          
    // Constructors          
    // =========================================================== 
    public FruitPool(final ITextureRegion watermelonRegion, ITextureRegion cherryRegion,ITextureRegion mBallTextureRegion, ITextureRegion grapeTextureRegion, ITextureRegion strawberryTextureRegion,Scene attachedScene) {
        this.mTextureRegions[0] = watermelonRegion;
        this.mTextureRegions[1] =cherryRegion;
        this.mTextureRegions[2]= mBallTextureRegion;
        this.mTextureRegions[3]= grapeTextureRegion;
        this.mTextureRegions[4] = strawberryTextureRegion;
        this.mScene = attachedScene;
    }
    // ===========================================================          
    // Getter & Setter          
    // =========================================================== 
    
    // ===========================================================          
    // Methods for/from SuperClass/Interfaces          
    // ===========================================================  
    @Override
    protected Sprite onAllocatePoolItem() {
        //This method is called internally by GenericPool<T> class.
        //It is called when the pool is out of items, so a new one should be allocated.
        //Remember - you MUST create a new item here! Don't return a reference to an existing one.
        int fruitPos = MathUtils.random(0, 4);
        final Sprite fruit = new Sprite(0, 0, this.mTextureRegions[fruitPos]);
        this.mScene.attachChild(fruit);
        return fruit;
    }
    @Override
    protected void onHandleObtainItem(final Sprite pItem) {
        //Before we return the sprite to the caller, we reset it's fields.
        //This method is called internaly by GenericPool<T> class.
        pItem.reset();
    }
    @Override
    protected void onHandleRecycleItem(final Sprite pItem) {
        //When an item is recycled, this method is called. We make it invisible and set it to ignores updates.
        //We DONT detach it from the scene, just make it ignore updates.
        //Again, this method is called internally by GenericPool<T>class.
        pItem.setVisible(false);
        pItem.setIgnoreUpdate(true);
    }
    // ===========================================================          
    // Methods          
    // ===========================================================  
    
    // ===========================================================          
    // Inner and Anonymous Classes          
    // ===========================================================  
    }
    

    Remember – you don’t attach/detach a sprite from the pool! It is attached once when it’s created.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have A WCF service that has a class that inherits System.Web.Security.RoleProvider. In this
I have created a static library following this link . But I am facing
I have created a custom tag that looks like this: def textField = {
I have a class that conforms to UISearchDisplayDelegate and contains a UISearchBar. This view
We have an MSI created by a programmer that left. It has a Installation
I have a property in one of the classes I created that currently has
Say I have a class DoStuff, and that class has two methods, like so
I have created this code, and when I run it, don't get any errors
I have created this report using BIRT and phpjavabridge <?php header(Content-type: application/pdf); header(Content-Disposition: inline;
I have created something like this and this . In effect I have a

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.