我想知道 Sprite Pool 是如何工作的,因为我不太了解它们。我要做的是在每次用户触摸按钮时显示随机精灵(我已经管理了控件),但我的代码似乎不正确,因为它只会一遍又一遍地显示相同的精灵。
这是我的代码:
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();
}
}
希望大家能帮帮我,谢谢:)