2011-12-22 25 views
1

我使用AndEngine创建游戏。AndEngine的GenericPool处理6 Sprites

我在我的代码中随机添加了精灵,它们几乎每秒都会产生一次。

我被告知使用Generic Pool会在我的情况下节省垃圾收集,导致我的游戏在特定时间滞后。

所以,这里是香港专业教育学院设法拿出我的GenericPool ..

public class FruitPool extends GenericPool<Sprite> { 
private Sprite msprite; 


public FruitPool(Sprite sprite) { 
if (sprite == null) { 
// Need to be able to create a Sprite so the Pool needs to have a TextureRegion 
throw new IllegalArgumentException("The texture region must not be NULL"); 
} 
msprite = sprite; 
} 

/** 
* Called when a Bullet is required but there isn't one in the pool 
*/ 
@Override 
protected Sprite onAllocatePoolItem() { 
return msprite; 

} 

/** 
* Called when a Bullet is sent to the pool 
*/ 
@Override 
protected void onHandleRecycleItem(final Sprite sprite) { 
msprite = sprite; 
msprite.setIgnoreUpdate(true); 
msprite.setVisible(false); 
} 

/** 
* Called just before a Bullet is returned to the caller, this is where you write your initialize code 
* i.e. set location, rotation, etc. 
*/ 
@Override 
protected void onHandleObtainItem(final Sprite fruit) { 
fruit.reset(); 
} 
} 

所以当你们看到香港专业教育学院创建的,我能够加入雪碧到池。

问题是我有一个方法,随机选择1和6之间的数字。我使用开关统计来选择哪个精灵将被添加到场景中。

我怎么能用GenericPool做到这一点?拥有六个不同的精灵,并能够选择哪一个添加到场景中?

我在想也许我可以创建一个方法,将每个雪碧添加到我的游戏池中,然后我坚持在我找到一种方法来选择哪个精灵从池中选择的部分,如提供一个int来挑选一个精灵。

感谢您在Advance的帮助!

回答

2

首先,这是而不是池如何工作。 在池中,每次调用obtain应获得另一个未回收的对象。这意味着:

FruitPool pool = new FruitPool(...); 
Sprite sprite1 = pool.obtain(); 
Sprite sprite2 = pool.obtain(); 

现在,sprite1sprite2应该引用同一个对象。但随着你的实施,他们,因此它将不会工作,当你将获得并同时使用2项。

你应该实现这样的:

public class FruitPool extends GenericPool<Sprite> { 
// =========================================================== 
// Constants   
// =========================================================== 

// ===========================================================   
// Fields   
// =========================================================== 
private final TextureRegion mTextureRegion; 
// ===========================================================   
// Constructors   
// =========================================================== 
public FruitPool(final TextureRegion pFruitTextureRegion) { 
    this.mTextureRegion = pFruitTextureRegion; 
} 
// ===========================================================   
// Getter & Setter   
// =========================================================== 

// ===========================================================   
// Methods for/from SuperClass/Interfaces   
// =========================================================== 
@Override 
protected Sprite onAllocatePoolItem() { 
    return new Sprite(0, 0, this.mTextureRegion); 
} 
@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   
// =========================================================== 
} 

这种方式,多精灵可以从池要求在同一时间,和游泳池实际上将填补它的目的。

关于你的问题,一个池使用堆栈实现,所以你不能拿出它的一个特定项目 - 只有顶部的一个。我建议你然后创建6个不同的水果池,每个水果池用于另一种水果类型。

你应该可以扩展Sprite和包括场type,当前水果的类型,所以你知道哪个水果池,你应该把它当您完成,或使用setUserDatagetUserData的方式来保存有关当前水果类型的信息。

+1

哇,非常感谢Jong!我现在明白它好多了!所以基本上我将纹理区域提供给池,我可以使用poolItem.obtainItem()来获得我的第一个水果。我只需要为每个池创建6个不同的池。现在如果其中一个脱离屏幕,它是如何工作的?回收利用如何工作?并再次使用相同的精灵? – 2011-12-22 16:26:35

+1

当您完成一个精灵(例如,它离开屏幕)时,您将其称为池方法'recyclePoolItem'。现在,池保持这个精灵,下一次你调用'obtainPoolItem'时,它将返回它而不是分配一个新的(这就是池的目的) – Jong 2011-12-22 18:14:19

+1

非常感谢Jong。你很帮忙!谢谢!! – 2011-12-22 22:09:40