Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice to use Sprites in a game using AndEngine GLES2

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.

like image 304
Waqas Avatar asked Jul 16 '12 10:07

Waqas


1 Answers

Is there any reason you need to load the textures at runtime? The normal way is to load the required textures all onto a single atlas while loading the application so that you can then quickly use them later.

As for the code reusability, Todilo's idea about enums seems to be pretty much what you need. Say for example that you have two kinds of objects - objects that disappear when you touch them and objects that fly up when you touch them. You enumerate both categories and put a piece of code into the touch event handling code that checks whether the object should disappear or fly up.

If you don't know what the objects should be doing on touch before running the application, there is a more dynamic way of achieving the same result. Just create two lists at runtime and put a reference to the object in one of the lists according to what the object should do when touched. Then in touch event handling do something like this:

if (disappearList.contains(touchedObject)) {
    disappear(object)
}
if (flyUpList.contains(touchedObject)) {
    flyUp(object)
}

Too bad AndEngine does not allow users to set listeners on sprites, it would make things a bit easier.

EDIT: Added explanation of the use of BlackPawnTextureBuilder: Your Atlas must be of type BuildableBitmapTextureAtlas, then you add all textures like this

BitmapTextureAtlasTextureRegionFactory.createFromAsset(buildableAtlas, this, "image.png"));

and after that

try {
    this.buildableAtlas.build(new BlackPawnTextureBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(1));
} catch (final TextureAtlasSourcePackingException e) {
    Debug.e(e);
}

I don't know whether this will work for animated Sprites or not, you will have to try it. Also, there is no overriding onTouch, you will have to do that in the onAreaTouched method. One example of such condition is

if (pSceneMotionEvent.getAction() == MotionEvent.ACTION_DOWN && disappearList.contains(pTouchArea)) {disappear();}
like image 132
JohnEye Avatar answered Nov 19 '22 03:11

JohnEye