0
votes

I created this pool to recycled and reuse sprites that i add to my scene

As you will see in my code i created my own obtain method that randomizes what sprite is chosen to appear on the scene, and randomize its x position. The problem is that it seems to pick the same position for some reason, it picks the middle,left, and right for some reason during the whole scene, when it is suppose to randomize the positions, each time i restart the scene it randomly picks 3 positions for 3 sprites and stays the same all through out the scene, the sprites arent randomized either. Here is what im working with

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;
// ===========================================================          
// Constructors          
// =========================================================== 
public FruitPool(final ITextureRegion pFruitTextureRegion,
        ITextureRegion pFruitTextureRegion2,ITextureRegion pFruitTextureRegion3, ITextureRegion pFruitTextureRegion4, ITextureRegion pFruitTextureRegion5,Scene mScene2, Camera camera, LinkedList<Sprite>items) {

    this.texture1 = pFruitTextureRegion;
    this.texture2 =pFruitTextureRegion2;
    this.texture3 = pFruitTextureRegion3;
    this.texture4 = pFruitTextureRegion4;
    this.texture5 = pFruitTextureRegion5;
    this.mScene = mScene2;
    this.pool1 = items;

    this.mCamera = camera;

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

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

     Random rand = new Random();
     Random randFruit = new Random();
     Sprite fruit = null;
      float x = rand.nextInt((int) mCamera.getWidth() - texture3.getHeight());

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

     switch(textureNumber){
     case 1:
          fruit = new Sprite(x, 0, this.texture1);
          break;
     case 2:
         fruit = new Sprite(x, 0, this.texture2);
         break;
     case 3:
         fruit = new Sprite(x, 0, this.texture3);
         break;
     case 4:
         fruit = new Sprite(x, 0, this.texture4);
         break;
     case 5:
         fruit = new Sprite(x, 0, this.texture5);
         break;

     }


    mScene.attachChild(fruit);

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

Anything that i can do or change to make the logic of this better?

EDIT:

Here is what ive managed to come up with so far, now im stuck on how do i pull a item out and then recycle it to be resued

public class sprites extends TObjectPool{

private CustomArrayList<Sprite>  sprites;

private  ITextureRegion texture1;
private  ITextureRegion texture2;
private ITextureRegion texture3;
private  ITextureRegion texture4;
private  ITextureRegion texture5;


public sprites(int poolsize) {
    super(poolsize);

    sprites = new CustomArrayList<Sprite>(poolsize);

    // TODO Auto-generated constructor stub
}

public void FruitPool(final ITextureRegion watermelonRegion,
        ITextureRegion cherryRegion,ITextureRegion mBallTextureRegion, ITextureRegion grapeTextureRegion, ITextureRegion strawberryTextureRegion) {

    texture1 = watermelonRegion;
    texture2 = cherryRegion;
    texture3 = mBallTextureRegion;
    texture4 = grapeTextureRegion;
    texture5 = strawberryTextureRegion;






     }




@Override
public void fillPool() {
     Sprite fruit1 = new Sprite(0, 0, this.texture1);
     sprites.add(fruit1);

     Sprite fruit2 = new Sprite(0, 0, this.texture2);
     sprites.add(fruit2);


    Sprite  fruit3 = new Sprite(0, 0, this.texture3);
    sprites.add(fruit3);


    Sprite  fruit4 = new Sprite(0, 0, this.texture4);
    sprites.add(fruit4);


     Sprite fruit5 = new Sprite(0, 0, this.texture5);
     sprites.add(fruit5);



      super.getAvailable().add(fruit1);
      super.getAvailable().add(fruit2);
      super.getAvailable().add(fruit3);
      super.getAvailable().add(fruit4);
      super.getAvailable().add(fruit5);



}

}

I pull an item out like this..

  sprites spritesClass = sprites(5);
  Sprite item = spritesClass.allocate();

How do i recycle items? Am i doing this right?

1

1 Answers

1
votes

Does onAllocatePoolItem() gets called frequently throughout your game? If so, that's not an object pool by itself. In fact, in the current case you are creating new objects every time you call the method.

Instead of trying to help you with your "problem", I'll just point to an open source project by me, which shows you how to properly setup and use an object pool for your game. The project includes a randomizing feature. The comments are in Swedish, but you should be able to figure that part out anyway.