0

我想知道 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();
    }
}

希望大家能帮帮我,谢谢:)

4

1 回答 1

0

我将从我的应用程序中向您展示一个简单的奶牛池,让您了解池的工作原理。我的 CowPool 用作生成 CowCritters(四处走动、吃草并通常做你期望奶牛做的事情的 NPC 奶牛)的来源。这是代码:

public class CowPool extends GenericPool<CowCritter> {
private final String        TAG = this.getClass().getSimpleName();

public CowPool() {
    super();
}

@Override
protected CowCritter onAllocatePoolItem() {
    return new CowCritter();
}

protected void recycle(CowCritter cow) {
    this.recyclePoolItem(cow);
}

}

您会看到有两种方法,一种是分配池项目(生成一头新奶牛),另一种是回收奶牛。当我需要一头牛时,我不会调用这两种方法,而是调用cowPool.obtainPoolItem()。如果池中有奶牛,它将归还奶牛。如果没有,它将调用 onAllocatePoolItem(),创建一头新奶牛,并将返回该奶牛。当我处理完一头奶牛后,我使用 recycle() 方法将它扔回池中。

这一切有什么意义?

好吧,首先请注意,我不必做任何这些。相反,我可以在需要时实例化一头新牛,然后将其丢弃。要理解的关键点是,当我实例化一头新奶牛时,会有开销。它分配内存资源。等等。同样,当我处理一头牛时,它也有资源。在某些时候,垃圾收集将不得不清理所说的牛,这需要一点处理时间。

池化本质上只是一种回收形式。我知道我将来会再次需要一头牛,所以我没有永久处理这头牛,而是把它放在水池里,然后,当我需要一头牛时,牛就在我身边。不涉及垃圾收集,因为池正在保留额外的牛。并且实例化一头新奶牛更快,因为我并没有真正实例化一头新奶牛,奶牛已经在池中(除非奶牛不在,然后它会制造一头奶牛)。

理解池化是一种优化形式也很重要。您不会通过池化获得新功能;相反,您可能会更智能地处理资源。我说可能,因为池化对象并不总是有意义的。

I would suggest that you avoid pooling just for pooling's sake. In other words, make sure you're addressing an actual issue. Profile your code and find out where the real bottlenecks are. If the creation or disposal of objects is taking real time or memory, you may want to start pooling. A perfect opportunity for pooling would be bullets. Imagine you're spraying tons of bullets, creating new ones and disposing of the old ones. In such a case, you might get a real performance benefit by pooling instead.

Hope this helps.

于 2013-03-15T18:52:26.247 回答