私はこのジェネリックプールを作成して、シーンに追加するスプライトを再利用しました。プールを使ってスプライトをリサイクルして再利用
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;
// ===========================================================
// 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 rand = new Random();
Random randFruit = new Random();
Sprite fruit = null;
float x = rand.nextInt((int) mCamera.getWidth() - texture1.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
// ===========================================================
コードがうまく動作しますが、それはリサイクルスプライトを再利用しますが、新しいものを毎回割り当てているように見えるdoesntの..
私は、任意の提案
face = fruitsPool.onAllocatePoolItem();
を呼び出すことによって、スプライトを取得しますか?
編集:私はこのようにそれを設定するの答えのメソッドを試してみました
...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 = null;
private static Sprite fruitTwo= null;
private static Sprite fruitThree = null;
private static Sprite fruitFour = null;
private static Sprite fruitFive = null;
private Sprite fruit = null;
// ===========================================================
// 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
// ===========================================================
}
と私は
Sprite sprite = fruitPool.onAllocatePoolItem();
then i attach it to the scene(this method i use is called about every second)
を呼び出すしかし、周りのたびに私はエラーを取得することエンティティ(スプライト)には既に親があり、添付されています。
またはスプライトはnullを返すだけです。
間違いや不足していることがありますか?
は、それは常に同じ親に添付されていますか? – Deco
さて、今度はそれを修正しました。私は最初からシーンから切り離すことはありませんでした。 –