Currently I am having static reference to all my sprites and loading and initializing them in my OnCreateResource mthod of SimpleBaseGameActivity, But now I have to override onAreaTouched listener on spirtes and the way I can override it while Initializing the Sprite. But I have a static method creating Atlas and Texture Region for every sprite. And I am using these sprites in my scene class and I want to override onAreaTouched there. I can registerTouchArea for that specific sprite in my scene so that can be done But I want to Override OnAreaTouched in a way so that Code reusability can be done. Here is how I am currently creating and loading sprites.

defualtCageSprite = createAndLoadSimpleSprite("bg.png", this, 450, 444);

And this is my Method createAndLoadSimpleSprite.

public static Sprite createAndLoadSimpleSprite(String name,
    SimpleBaseGameActivity activity, int width, int height) {

BitmapTextureAtlas atlasForBGSprite = new BitmapTextureAtlas(
        activity.getTextureManager(), width, height);
TextureRegion backgroundSpriteTextureRegion = BitmapTextureAtlasTextureRegionFactory
        .createFromAsset(atlasForBGSprite, activity, name, 0, 0);
Sprite sprite = new Sprite(0, 0, backgroundSpriteTextureRegion,

return sprite;

Now How Can I override onAreaTouched for some sprites while not losing the code reusability.


1 Answer 1


You should definitely detach creation of Sprite and Texture. Unless you are working with strictly 1:1 ratio - each sprite you create has different texture - and you create all your sprites and the beginning. The operation to load the texture from file to memory is resource intensive. The operation to assign texture to a sprite is almost free.

Therefore you should create textures only once at the start, before any sprites are created in some "loading" phase. Then create the sprites. You can have specialized sprites, like "BulletSprite" that will always show the same texture, so you don't have to pass it in constructor.

Instead of using createAndLoadSimpleSprite, consider implementing a sprite factory using this pattern: http://en.wikipedia.org/wiki/Factory_method_pattern

The best practice is always to load all textures first, then create sprites. Usually you will have more sprites sharing a texture. Drawing a Sprite is just an instruction - draw this texture there. In fact, textures are stored in completely separate memory, they are not stored on Java heap.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .