GenericPool逻辑问题(AndEngine)

发布于 2024-12-24 16:02:14 字数 4692 浏览 3 评论 0原文

我创建了这个池来回收和重用添加到场景中的精灵,

正如您将在我的代码中看到的那样,我创建了自己的获取方法,该方法随机化选择出现在场景中的精灵,并随机化其 x 位置。问题是,它似乎出于某种原因选择了相同的位置,它在整个场景中出于某种原因选择了中间、左边和右边,当它应该随机化位置时,每次我重新启动场景时它都会随机选择3 个精灵有 3 个位置,并且在整个场景中保持不变,精灵也不是随机的。这是我正在做的

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 pFruitTextureRegion,
        ITextureRegion pFruitTextureRegion2,ITextureRegion pFruitTextureRegion3, ITextureRegion pFruitTextureRegion4, ITextureRegion pFruitTextureRegion5,Scene mScene2, Camera camera, LinkedList<Sprite>items) {

    this.texture1 = pFruitTextureRegion;
    this.texture2 =pFruitTextureRegion2;
    this.texture3 = pFruitTextureRegion3;
    this.texture4 = pFruitTextureRegion4;
    this.texture5 = pFruitTextureRegion5;
    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() - texture3.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          
// ===========================================================  
}

事情我可以做或改变以使其逻辑更好吗?

编辑:

这是我到目前为止设法想出的,现在我陷入了如何拉出一个项目然后回收它以重新使用的问题

public class sprites extends TObjectPool{

private CustomArrayList<Sprite>  sprites;

private  ITextureRegion texture1;
private  ITextureRegion texture2;
private ITextureRegion texture3;
private  ITextureRegion texture4;
private  ITextureRegion texture5;


public sprites(int poolsize) {
    super(poolsize);

    sprites = new CustomArrayList<Sprite>(poolsize);

    // TODO Auto-generated constructor stub
}

public void FruitPool(final ITextureRegion watermelonRegion,
        ITextureRegion cherryRegion,ITextureRegion mBallTextureRegion, ITextureRegion grapeTextureRegion, ITextureRegion strawberryTextureRegion) {

    texture1 = watermelonRegion;
    texture2 = cherryRegion;
    texture3 = mBallTextureRegion;
    texture4 = grapeTextureRegion;
    texture5 = strawberryTextureRegion;






     }




@Override
public void fillPool() {
     Sprite fruit1 = new Sprite(0, 0, this.texture1);
     sprites.add(fruit1);

     Sprite fruit2 = new Sprite(0, 0, this.texture2);
     sprites.add(fruit2);


    Sprite  fruit3 = new Sprite(0, 0, this.texture3);
    sprites.add(fruit3);


    Sprite  fruit4 = new Sprite(0, 0, this.texture4);
    sprites.add(fruit4);


     Sprite fruit5 = new Sprite(0, 0, this.texture5);
     sprites.add(fruit5);



      super.getAvailable().add(fruit1);
      super.getAvailable().add(fruit2);
      super.getAvailable().add(fruit3);
      super.getAvailable().add(fruit4);
      super.getAvailable().add(fruit5);



}

}

我像这样拉出一个项目..

  sprites spritesClass = sprites(5);
  Sprite item = spritesClass.allocate();

如何做我回收物品?我这样做对吗?

I created this pool to recycled and reuse sprites that i add to my scene

As you will see in my code i created my own obtain method that randomizes what sprite is chosen to appear on the scene, and randomize its x position. The problem is that it seems to pick the same position for some reason, it picks the middle,left, and right for some reason during the whole scene, when it is suppose to randomize the positions, each time i restart the scene it randomly picks 3 positions for 3 sprites and stays the same all through out the scene, the sprites arent randomized either. Here is what im working with

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 pFruitTextureRegion,
        ITextureRegion pFruitTextureRegion2,ITextureRegion pFruitTextureRegion3, ITextureRegion pFruitTextureRegion4, ITextureRegion pFruitTextureRegion5,Scene mScene2, Camera camera, LinkedList<Sprite>items) {

    this.texture1 = pFruitTextureRegion;
    this.texture2 =pFruitTextureRegion2;
    this.texture3 = pFruitTextureRegion3;
    this.texture4 = pFruitTextureRegion4;
    this.texture5 = pFruitTextureRegion5;
    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() - texture3.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          
// ===========================================================  
}

Anything that i can do or change to make the logic of this better?

EDIT:

Here is what ive managed to come up with so far, now im stuck on how do i pull a item out and then recycle it to be resued

public class sprites extends TObjectPool{

private CustomArrayList<Sprite>  sprites;

private  ITextureRegion texture1;
private  ITextureRegion texture2;
private ITextureRegion texture3;
private  ITextureRegion texture4;
private  ITextureRegion texture5;


public sprites(int poolsize) {
    super(poolsize);

    sprites = new CustomArrayList<Sprite>(poolsize);

    // TODO Auto-generated constructor stub
}

public void FruitPool(final ITextureRegion watermelonRegion,
        ITextureRegion cherryRegion,ITextureRegion mBallTextureRegion, ITextureRegion grapeTextureRegion, ITextureRegion strawberryTextureRegion) {

    texture1 = watermelonRegion;
    texture2 = cherryRegion;
    texture3 = mBallTextureRegion;
    texture4 = grapeTextureRegion;
    texture5 = strawberryTextureRegion;






     }




@Override
public void fillPool() {
     Sprite fruit1 = new Sprite(0, 0, this.texture1);
     sprites.add(fruit1);

     Sprite fruit2 = new Sprite(0, 0, this.texture2);
     sprites.add(fruit2);


    Sprite  fruit3 = new Sprite(0, 0, this.texture3);
    sprites.add(fruit3);


    Sprite  fruit4 = new Sprite(0, 0, this.texture4);
    sprites.add(fruit4);


     Sprite fruit5 = new Sprite(0, 0, this.texture5);
     sprites.add(fruit5);



      super.getAvailable().add(fruit1);
      super.getAvailable().add(fruit2);
      super.getAvailable().add(fruit3);
      super.getAvailable().add(fruit4);
      super.getAvailable().add(fruit5);



}

}

I pull an item out like this..

  sprites spritesClass = sprites(5);
  Sprite item = spritesClass.allocate();

How do i recycle items? Am i doing this right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不疑不惑不回忆 2024-12-31 16:02:14

onAllocatePoolItem() 在您的游戏中是否被频繁调用?如果是这样,那么它本身就不是一个对象池。事实上,在当前情况下,每次调用该方法时都会创建新对象。

我没有试图帮助您解决“问题”只需指向我的一个开源项目,它向您展示如何为您的游戏正确设置和使用对象池。该项目包括随机化功能。注释是瑞典语的,但无论如何你应该能够弄清楚那部分。

Does onAllocatePoolItem() gets called frequently throughout your game? If so, that's not an object pool by itself. In fact, in the current case you are creating new objects every time you call the method.

Instead of trying to help you with your "problem", I'll just point to an open source project by me, which shows you how to properly setup and use an object pool for your game. The project includes a randomizing feature. The comments are in Swedish, but you should be able to figure that part out anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文