2013-02-21 13 views
1

我正在AndEngine for Android中开发游戏。在我的游戏中,我必须在每秒钟后创建不同类型的Tiles(AnimatedSprite)。我已经做到了。但我在我的比赛中感觉混乱并且落后。我认为这是由于经常分配和取消分配对象造成的。所以我想在我的游戏中实现对象池模式。我现在创建瓷砖代码:Android在Andengine中为不同类型的实体实现对象池

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); 
} 

}

回答

2

退房....

org.andengine.util.adt.pool.MultiPool

这正是你想要的。一池游泳池。您需要使用您自己的自定义实现来扩展MultiPool。这是我在我的项目中使用的示例。 (我已经削减在multipool池的数量,但这样你就得到一个想法足够的离开了。)

public class CritterPool extends MultiPool { 
    private WorldActivity mContext; 

    public static enum TYPE { 
     PROTOGUY, COW, MALE, SKELETON 
    } 

    public CritterPool(WorldActivity mContext) { 
     super(); 
     this.mContext = mContext; 
     this.registerPool(TYPE.PROTOGUY.ordinal(), new ProtoGuyPool(mContext)); 
     this.registerPool(TYPE.COW.ordinal(), new CowPool(mContext)); 
     this.registerPool(TYPE.MALE.ordinal(), new MalePool(mContext)); 
     this.registerPool(TYPE.SKELETON.ordinal(), new SkeletonPool(mContext)); 
    } 

    public BaseCritter get(TYPE type) { 
     return (BaseCritter) this.obtainPoolItem(type.ordinal()); 
    } 

    public void recycle(BaseCritter critter) { 
     this.recyclePoolItem(critter.getType().ordinal(), critter); 
    } 

} 

所以,现在,从外面看,我创建了multipool,然后我可以调用get ()与任何类型的(在这种情况下是“小动物”)我想从游泳池,无论是牛,骨架等

+1

是的。这正是我所期待的。谢谢... – 2013-02-22 07:17:43

+0

@KhawarRaza无论出于何种原因,我发现使用游戏中的所有内容制作一个多池游戏是非常令人满意的。 “Multipool,给我一个,呃,恐龙。” – 2013-02-22 08:07:51

+0

我正面临着一个问题,我收到这条消息:“com.android.mygame.StaticTilePool 已经用完了,有65个项目还没有回收,还分配了1个。”即使我定期回收物品,但不应超过每个场景40多个物品。我更新了我的问题。请参阅StaticTilePool类。你能看到出了什么问题吗? – 2013-02-22 10:12:39

相关问题