我正在使用 andengine 开发一个游戏,其中我正在使用通用池来处理不同的精灵,现在我的游戏类似于水果忍者,其中水果会在屏幕上弹出,但在一段时间或 15 分钟后游戏变得缓慢,因为我正在使用
TargetPool tPool;
tPool = new TargetsPool(apple,watermelon,lemon,mango,tomato);
target=tPool.onAllocatePoolItem();
tPool.onHandleRecycleItem(target);
public class TargetsPool extends GenericPool<Sprite> {
private ITextureRegion mTextureRegion, mTextureRegion1,mTextureRegion2, mTextureRegion3,mTextureRegion4;
public TargetsPool(ITextureRegion pTextureRegion1,ITextureRegion pTextureRegion2,ITextureRegion pTextureRegion3,ITextureRegion pTextureRegion4,ITextureRegion pTextureRegion5) {
if (pTextureRegion1 == null&&pTextureRegion2 == null) {
throw new IllegalArgumentException(
"The texture region must not be NULL");
}
mTextureRegion = pTextureRegion1;
mTextureRegion1 = pTextureRegion2;
mTextureRegion2 = pTextureRegion3;
mTextureRegion3 = pTextureRegion4;
mTextureRegion4 = pTextureRegion5;
}
@Override
protected Sprite onAllocatePoolItem() {
Random r = new Random();
int a =r.nextInt(5);
//Log.e(Integer.toString(a),"a" );
Sprite sprite;
VertexBufferObjectManager getvertex = null;
switch(a){
case 0:
sprite =new Sprite(0, 0, mTextureRegion,getvertex);
break;
case 1:
sprite =new Sprite(0, 0, mTextureRegion1,getvertex);
break;
case 2:
sprite =new Sprite(0, 0, mTextureRegion2,getvertex);
break;
case 3:
sprite =new Sprite(0, 0, mTextureRegion3,getvertex);
break;
case 4:
sprite =new Sprite(0, 0, mTextureRegion4,getvertex);
break;
default:sprite =new Sprite(0, 0, mTextureRegion,getvertex);
}
return sprite;
}
protected void onHandleRecycleItem(final Sprite target) {
target.clearEntityModifiers();
target.clearUpdateHandlers();
target.setVisible(false);
target.detachSelf();
target.reset();
}
}
我想知道如何再次从池中回收目标项目,就好像每次游戏变慢时我都会生成新目标一样