我是Android AndEngine游戏开发的新手。我正在开发一个游戏,我使用GenericPool在屏幕上加载多个spites。问题是这些精灵是同一类型。我想加载不同类型的精灵。经过一些研究后我得到了使用Multipool的想法,但不知道如何在我的代码中使用它。
这是我的代码:
public class Enemy {
public Sprite sprite;
public int hp;
// the max health for each enemy
protected final int MAX_HEALTH = 2;
Camera mCamera;
BaseActivity activity = BaseActivity.getSharedInstance();
public Enemy() {
sprite = new Sprite(32, 32, BaseActivity.getSharedInstance().mBirdTextureRegion,
BaseActivity.getSharedInstance().getVertexBufferObjectManager());
init();
}
// method for initializing the Enemy object , used by the constructor and
// the EnemyPool class
public void init() {
hp = MAX_HEALTH;
mCamera = activity.mCamera;
}
public void clean() {
sprite.clearEntityModifiers();
sprite.clearUpdateHandlers();
}
// method for applying hit and checking if enemy died or not
// returns false if enemy died
public boolean gotHit() {
synchronized (this) {
hp--;
if (hp <= 0)
return false;
else
return true;
}
}
public class EnemyLayer extends Entity {
private LinkedList<Enemy> enemies;
public static EnemyLayer instance;
public int enemyCount;
public static EnemyLayer getSharedInstance() {
return instance;
}
public static boolean isEmpty() {
if (instance.enemies.size() == 0)
return true;
return false;
}
public static Iterator<Enemy> getIterator() {
return instance.enemies.iterator();
}
public void purge() {
detachChildren();
for (Enemy e : enemies) {
EnemyPool.sharedEnemyPool().recyclePoolItem(e);
}
enemies.clear();
}
public EnemyLayer(int x) {
enemies = new LinkedList<Enemy>();
instance = this;
enemyCount = 4;
}
public void restart() {
Log.v("jimvaders", "EnemyLayer restarted");
enemies.clear();
clearEntityModifiers();
clearUpdateHandlers();
for (int i = 0; i < enemyCount; i++) {
Enemy e = EnemyPool.sharedEnemyPool().obtainPoolItem();
float finalPosX = (i % 6) * 2 * e.sprite.getWidth()/2 * 3/2;
float finalPosY = ((int) (i / 6)) * e.sprite.getHeight() * 2;
Random r = new Random();
e.sprite.setPosition(r.nextInt(2) == 0 ? -e.sprite.getWidth() * 3
: BaseActivity.CAMERA_WIDTH + e.sprite.getWidth() * 3,
(r.nextInt(5) + 1) * e.sprite.getHeight());
e.sprite.setVisible(true);
attachChild(e.sprite);
e.sprite.registerEntityModifier(new MoveModifier(1,
e.sprite.getX(), finalPosX, e.sprite.getY(), finalPosY));
enemies.add(e);
}
setVisible(true);
setPosition(50, 30);
MoveXModifier movRight = new MoveXModifier(1, 50, 120);
MoveXModifier movLeft = new MoveXModifier(1, 120, 50);
MoveYModifier moveDown = new MoveYModifier(1, 30, 100);
MoveYModifier moveUp = new MoveYModifier(1, 100, 30);
registerEntityModifier(new LoopEntityModifier(
new SequenceEntityModifier(movRight, moveDown, movLeft, moveUp)));
}
public static void purgeAndRestart() {
Log.v("Birds Shooter", "EnemyLayer PurgeAndRestart()");
instance.purge();
instance.restart();
}
@Override
public void onDetached() {
purge();
clearUpdateHandlers();
super.onDetached();
}
public class EnemyPool extends GenericPool<Enemy> {
public static EnemyPool instance;
public static EnemyPool sharedEnemyPool() {
if (instance == null)
instance = new EnemyPool();
return instance;
}
private EnemyPool() {
super();
}
@Override
protected Enemy onAllocatePoolItem() {
return new Enemy();
}
@Override
protected void onHandleObtainItem(Enemy pItem) {
pItem.init();
}
protected void onHandleRecycleItem(final Enemy e) {
e.sprite.setVisible(false);
e.sprite.detachSelf();
e.clean();
}
}
如何修改此代码以使用不同类型intead相同类型的多个sprite。?????任何帮助将不胜感激。
答案 0 :(得分:0)
您可以使用自定义Sprite对象扩展Sprite类并创建新池。例如:
public class FlyingEnemy extends Sprite
{
//Class Body Here
}
一旦覆盖了Sprite类以满足您的需求,您只需扩展通用池以处理新的Sprites
public class FlyingEnemyPool extends GenericPool<FlyingEnemy>
{
//Class Body Here
}
然后你就像平常一样使用新的游泳池。