Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have read up on a tutorial that allows you to reuse sprites that are re-added to the scene such as bullets from a gun or any other objects using an ObjectPool.

In my game i have a variation of sprites about 6 all together with different textures.

This is how the object pool is set up with its own class extending Java's GenericPool class

public class BulletPool extends GenericPool<BulletSprite> {
private TextureRegion mTextureRegion;

 public BulletPool(TextureRegion pTextureRegion) {
  if (pTextureRegion == 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");
 }
 mTextureRegion = pTextureRegion;
} 

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

/**
 * Called when a Bullet is sent to the pool
*/
@Override
protected void onHandleRecycleItem(final BulletSprite pBullet) {
pBullet.setIgnoreUpdate(true);
pBullet.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 BulletSprite pBullet) {
  pBullet.reset();
}
}

As you see here it takes a TextureRegion parameter. The only problem i am facing with this is that i need to have 6 different sprites recycled and reused in the ObjectPool. This ObjectPool is set up to only use one TextureRegion.

Any idea's or suggestions on how to do this?

share|improve this question

2 Answers 2

A bit late, but let's see.

I have never used AndEngine, but if you create a texture atlas with all your shot sprites, the single TextureRegion can determine which sprite to use for each.

A sprite atlas is a big texture which contains several inside it, usually tiled in a regular fashion. You can specify which one to paint each time by using different regions of the texture. For some more info, check this Gamasutra article: http://www.gamasutra.com/view/feature/2530/practical_texture_atlases.php

share|improve this answer

In AndEngine you need to textureRegion.clone() or textureRegion.deepCopy() depending on the version of AndEngine that you use.

This will make sure that each sprite uses independent graphics. Symptom if you don't is that all sprites will have same tile index for example or same modifiers applied.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.