我创建了这个包含5个静态变量的Pool。
public class FruitPool extends GenericPool<Sprite> {
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Fields
// ===========================================================
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;
private static Sprite fruitOne;
private static Sprite fruitTwo;
private static Sprite fruitThree;
private static Sprite fruitFour;
private static Sprite fruitFive;
private Sprite fruit;
// ===========================================================
// Constructors
// ===========================================================
public FruitPool(final ITextureRegion watermelonRegion,
ITextureRegion cherryRegion,ITextureRegion mBallTextureRegion, ITextureRegion grapeTextureRegion, ITextureRegion strawberryTextureRegion,Scene mScene2, Camera camera, LinkedList<Sprite>items) {
this.texture1 = watermelonRegion;
this.texture2 =cherryRegion;
this.texture3 = mBallTextureRegion;
this.texture4 = grapeTextureRegion;
this.texture5 = strawberryTextureRegion;
this.mScene = mScene2;
this.pool1 = items;
this.mCamera = camera;
}
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
protected Sprite onAllocatePoolItem() {
Random randFruit = new Random();
int textureNumber = randFruit.nextInt(5)+1;
switch(textureNumber){
case 1:
if (fruitOne == null) {
fruitOne = new Sprite(0, 0, this.texture1);
Log.e("FruitPool", "Item rremade");
} else {
fruit = fruitOne;
Log.e("FruitPool", "Item exist in pool..Used");
}
break;
case 2:
if(fruitTwo == null){
fruitTwo = new Sprite(0, 0, this.texture2);
}else{
fruit = fruitTwo;
Log.e("FruitPool", "Item exist in pool..Used");
}
break;
case 3:
if(fruitThree == null){
fruitThree = new Sprite(0, 0, this.texture3);
}else{
fruit = fruitThree;
Log.e("FruitPool", "Item exist in pool..Used");
}
break;
case 4:
if(fruitFour == null){
fruitFour = new Sprite(0, 0, this.texture4);
}else{
fruit = fruitThree;
Log.e("FruitPool", "Item exist in pool..Used");
}
break;
case 5:
if(fruitFive == null){
fruitFive = new Sprite(0, 0, this.texture5);
}else{
fruit = fruitFive;
Log.e("FruitPool", "Item exist in pool..Used");
}
break;
}
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
// ===========================================================
}
正如您在onAllocate方法中看到的那样,我检查项目是否存在,如果它存在,我会在方法中返回它。
所以在我的主要活动中我使用
face = fruitsPool.onAllocatePoolItem();
这最初有效,但问题是我每秒都将脸贴在场景上。并且我得到了精灵已经附加到场景的错误。起初,我发现这样做的唯一方法是每秒创建一个新的Sprite,并在我完成它时将其分离,但这会使用很多内存并导致延迟,然后冻结。
有没有人对我需要做什么或建议代码有什么指示?
答案 0 :(得分:2)
您从不致电onAllocatePoolItem
。
您应该从GenericPool<T>
课程外部调用的仅方法是:
FruitPool
获取泳池项目; 不 obtainPoolItem
。onAllocatePoolItem
回收物品。记得在完成该项目后调用它。recyclePoolItem
,但在您的情况下您不需要它。当您想要在某个点创建池项时,可以使用它;但是,启动batchAllocatePoolItems
不是一个繁重的过程,所以你不需要。你不应该调用任何其他方法。
同样,你打破了对象池的目的! Sprite
应始终返回全新的对象!不是现有的!这就是你得到
实体已有父母
错误。
顺便说一句,当我第一次为你构建这个类时,我在创建时将精灵附加到场景中。你为什么不现在这样做?
我已对其进行了编辑,并添加了一些评论。
onAllocatePoolItem
请记住 - 你不从池中附加/分离精灵!它在创建时附加一次。
答案 1 :(得分:1)
我不确定,如果这有帮助的话。看看这是否有帮助。你为什么需要一个LinkedList?你可以使用数组吗?
private final Fruit fruits[] = new Fruits[4];
//Initialize this in the constructor...
for(int i =0; i < fruits.length; i++) {
fruits[i] = new Fruits();
}
@Override
protected Sprite onAllocatePoolItem() {
Random randFruit = new Random();
int textureNumber = randFruit.nextInt(5)+1;
switch(textureNumber){
case 1:
if (fruitOne == null) {
fruitOne = fruits[textureNumber];
//new Sprite(0, 0, this.texture1);
//use setters to set the values here..
Log.e("FruitPool", "Item rremade");
} else {
fruit = fruits[textureNumber];
Log.e("FruitPool", "Item exist in pool..Used");
}
break;
case 2:
if(fruitTwo == null){
fruitTwo = fruits[textureNumber];
//new Sprite(0, 0, this.texture1);
//use setters to set the values here..
Log.e("FruitPool", "Item rremade");
}else{
fruit = fruits[textureNumber];
Log.e("FruitPool", "Item exist in pool..Used");
}
....