I wanna know how Sprite Pool works, since I dont really understand them. What I'm trying to do is show random sprites each time user touch the button (I already manage the control), but my code doesn't seem right, because it only shows the same sprite again and over again.
This is my code :
public class SpritePool extends GenericPool<Sprite> {
private ITextureRegion mTexture1, mTexture2, mTexture3, mTexture4, mTexture5;
private VertexBufferObjectManager mVertexBufferObjectManager;
private Sprite sprite = null;
public SpritePool(ITextureRegion pTextureRegion1, ITextureRegion pTextureRegion2, ITextureRegion pTextureRegion3
, ITextureRegion pTextureRegion4, ITextureRegion pTextureRegion5, VertexBufferObjectManager pVerTexBufferObjectManager){
this.mTexture1 = pTextureRegion1;
this.mTexture2 = pTextureRegion2;
this.mTexture3 = pTextureRegion3;
this.mTexture4 = pTextureRegion4;
this.mTexture5 = pTextureRegion5;
this.mVertexBufferObjectManager = pVerTexBufferObjectManager;
}
@Override
protected Sprite onAllocatePoolItem() {
YesOrNoActivity.setRoll_1(MathUtils.RANDOM.nextInt(5) + 1);
switch(YesOrNoActivity.getRoll_1()){
case 1:
sprite = new Sprite(0, 0, this.mTexture1, this.mVertexBufferObjectManager);
break;
case 2:
sprite = new Sprite(0, 0, this.mTexture2, this.mVertexBufferObjectManager);
break;
case 3:
sprite = new Sprite(0, 0, this.mTexture3, this.mVertexBufferObjectManager);
break;
case 4:
sprite = new Sprite(0, 0, this.mTexture4, this.mVertexBufferObjectManager);
break;
case 5:
sprite = new Sprite(0, 0, this.mTexture5, this.mVertexBufferObjectManager);
break;
}
return sprite;
}
public synchronized Sprite obtainPoolItem(final float pX, final float pY) {
Sprite sprite = super.obtainPoolItem();
sprite.setPosition(pX, pY);
sprite.setVisible(true);
sprite.setIgnoreUpdate(false);
sprite.setColor(1,1,1);
return sprite;
}
@Override
protected void onHandleRecycleItem(Sprite pItem) {
super.onHandleRecycleItem(pItem);
pItem.setVisible(false);
pItem.setIgnoreUpdate(true);
pItem.clearEntityModifiers();
pItem.clearUpdateHandlers();
}
}
Hope you guys can help me out, thanks :)