AndEngine for Android でゲームを開発しています。私のゲームでは、1 秒ごとにさまざまな種類のタイル (AnimatedSprite) を作成する必要があります。私はそれをしました。しかし、私は自分のゲームでぎくしゃくしたり遅れたりしています。オブジェクトの割り当てと割り当て解除が頻繁に行われるためだと思います。だから私は自分のゲームにオブジェクト プール パターンを実装したいと考えています。タイルを作成するための私の現在のコード:
public class TileFactory implements EntityFactory {
private static TileFactory tileFactory;
@Override
public AnimatedEntity createEntity(PlayLevelActivity mGameWorld, ResourceType type) {
// TODO Auto-generated method stub
ITiledTextureRegion textureRegion = ResourceManager.getTextureRegion(mGameWorld, type);;
switch (type) {
case STATIC_TILE:
return new StaticTile(mGameWorld, 0, 0, textureRegion.getWidth(), textureRegion.getHeight(),
BodyType.StaticBody, textureRegion, mGameWorld.getVertexBufferObjectManager());
case DYNAMIC_TILE :
return new DynamicTile(mGameWorld, 0, 0, textureRegion.getWidth(), textureRegion.getHeight(),
BodyType.StaticBody, textureRegion, mGameWorld.getVertexBufferObjectManager());
case DANGER_TILE:
return new DangerTile(mGameWorld, 0, 0, textureRegion.getWidth(), textureRegion.getHeight(),
BodyType.StaticBody, textureRegion, mGameWorld.getVertexBufferObjectManager());
case FIRE_TILE:
return new FireTile(mGameWorld, 0, 0, textureRegion.getWidth(), textureRegion.getHeight(),
BodyType.StaticBody, textureRegion, mGameWorld.getVertexBufferObjectManager());
case HIGH_JUMP_TILE:
return new HighJumpTile(mGameWorld, 0, 0, textureRegion.getWidth(), textureRegion.getHeight(),
BodyType.StaticBody, textureRegion, mGameWorld.getVertexBufferObjectManager());
case RISK_TILE:
return new RiskyTile(mGameWorld, 0, 0, textureRegion.getWidth(), textureRegion.getHeight(),
BodyType.StaticBody, textureRegion, mGameWorld.getVertexBufferObjectManager());
default :
return null;
}
}
public static TileFactory getIntance() {
if(tileFactory == null) {
tileFactory = new TileFactory();
}
return tileFactory;
}
}
AndEngine で ObjectPool を使用する例をいくつか見てきましたが、それらは単一のタイプのエンティティに対してのみ機能します。いくつかのタイプのエンティティを作成します。現在のシナリオを ObjecPool のものに変換する方法のガイドラインはありますか?
StaticTilePool クラス:
public class StaticTilePool extends GenericPool<StaticTile> {
PlayLevelActivity mGameWorld;
ITiledTextureRegion textureRegion;
public StaticTilePool(PlayLevelActivity mGameWorld) {
this.mGameWorld = mGameWorld;
textureRegion = ResourceManager.getTextureRegion(mGameWorld, ResourceType.STATIC_TILE);
}
/**
* Called when a Tile is required but there isn't one in the pool
*/
@Override
protected StaticTile onAllocatePoolItem() {
Log.d("count:", "count: " + getAvailableItemCount() + " , maximum count: " + getAvailableItemCountMaximum() + " , unrecycled count: " + getUnrecycledItemCount());
return new StaticTile(mGameWorld, -100, -100, textureRegion.getWidth(), textureRegion.getHeight(),
BodyType.StaticBody, textureRegion, mGameWorld.getVertexBufferObjectManager());
}
/**
* Called just before a Tile is returned to the caller, this is where you write your initialize code
* i.e. set location, rotation, etc.
*/
@Override
protected void onHandleObtainItem(final StaticTile pItem) {
pItem.reset();
pItem.mBody.setAwake(false);
}
/**
* Called when a Tile is sent to the pool
*/
@Override
protected void onHandleRecycleItem(final StaticTile pItem) {
Log.d("onHandle recycle", "onhandle recycle oitem");
pItem.mBody.setAwake(true);
pItem.setVisible(false);
pItem.setIgnoreUpdate(true);
}
}