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.

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,
        activity.getVertexBufferObjectManager());
activity.getTextureManager().loadTexture(atlasForBGSprite);

return sprite;

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

share|improve this question
add comment

1 Answer

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.

share|improve this answer
add comment

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.