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 6996699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:11:27+00:00 2026-05-27T20:11:27+00:00

I am using AndEngine to create a game. I have randomly added sprites in

  • 0

I am using AndEngine to create a game.

I have randomly added sprites in my code that spawn just about every second.

I was informed that using a Generic Pool would save garbage collection in my case thats causing my game to lag at certain times.

So, here is what ive managed to come up with for my GenericPool..

public class FruitPool extends GenericPool<Sprite> {
private Sprite msprite;


public FruitPool(Sprite sprite) {
if (sprite == null) {
// Need to be able to create a Sprite so the Pool needs to have a TextureRegion
throw new IllegalArgumentException("The texture region must not be NULL");
}
msprite = sprite;
}

/**
* Called when a Bullet is required but there isn't one in the pool
*/
@Override
protected Sprite onAllocatePoolItem() {
return msprite;

}

/**
* Called when a Bullet is sent to the pool
 */
 @Override
protected void onHandleRecycleItem(final Sprite sprite) {
 msprite = sprite;
 msprite.setIgnoreUpdate(true);
 msprite.setVisible(false);
 }

 /**
 * Called just before a Bullet is returned to the caller, this is where you write your initialize code
 * i.e. set location, rotation, etc.
 */
 @Override
 protected void onHandleObtainItem(final Sprite fruit) {
 fruit.reset();
 }
 }

So as you guys see ive created a pool that i am able to add a Sprite to.

The problem is i have a method that randomly picks a number between 1 and 6. And i use a Switch statment to chose which sprite will be added the the scene.

How could i do this with the GenericPool? Having it hold six different sprites and being able to choose which one is added to the scene?

I was thinking maybe i could create a method that will add each Sprite to the Pool in my game, and then im stuck at the part where i find a way to select which sprite is selected out of the pool, such as providing a int that it takes to pick a sprite.

Thanks for the help in Advance guys!

  • 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-27T20:11:27+00:00Added an answer on May 27, 2026 at 8:11 pm

    First of all, that is not how pools work.
    In a pool, each call to obtain should obtain another un-recycled object. That means:

    FruitPool pool = new FruitPool(...);
    Sprite sprite1 = pool.obtain();
    Sprite sprite2 = pool.obtain();
    

    Now, sprite1 and sprite2 should not reference the same object. But with your implementation, they do and therefore it won’t work when you will obtain and use 2 items simultaneously.

    You should implement it like this:

    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(false);
        pItem.setIgnoreUpdate(true);
    }
    // ===========================================================          
    // Methods          
    // ===========================================================  
    
    // ===========================================================          
    // Inner and Anonymous Classes          
    // ===========================================================  
    }
    

    This way, multiple sprites can be requested from the pool at the same time, and the pool will actually fill it’s purpose.

    About your question, a pool is implemented using a stack, so you can not take out a specific item of it – only the one at the top. I recommend you then to create 6 different fruit pools, each one for another fruit type.

    You should either extend Sprite and include a field type, for the type of the current fruit, so you’d know to which fruit pool you should send it when you are done, or use the setUserData and getUserData methods to save information about the type of the current fruit.

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

Sidebar

Related Questions

I'm developing a physics game using AndEngine with box2d physics engine. I have a
I'm using AndEngine multi-touch functions to create multi-sprite for my game. But I've a
I am using java + AndEngine in my game. During the game i have
I am using AndEngine for my game. In that I want to give option
I am using this method i created to randomly create different sprites to be
I'm using AndEngine to create a Live Wallpaper and I have everything set up
I am new to android... I want to create simple android game using Andengine.
I am using AndEngine to create a Test run project. I am doing well
I am using angengine to create a project for school. I have a Sprite
I am using andengine to develop a game in android. I placed an object

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.