11

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.

Waqas
  • 4,411
  • 6
  • 27
  • 31

2 Answers2

8

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();}
JohnEye
  • 6,436
  • 4
  • 41
  • 67
  • 1
    what should be the atlas size then if I load all required textures to a single atlas? What will be benefit of using a single atlas, and by the way what do you mean by run time, I initialize all the sprites on createResources of my Engine then use them later in my application. – Waqas Jul 23 '12 at 09:56
  • 1
    And I just don't have to animate sprites on onTouch, I have to call different methods of my Model according to different sprites. – Waqas Jul 23 '12 at 10:03
  • 1
    The atlas size depends on the size of images you use, for example, I need 1024x512. The benefit is smaller memory consumption and faster rendering. By runtime I meant the time when the application runs. You can use BlackPawnTextureBuilder which will place the textures onto the atlas so that you don"t have to do that manually. Using my approach you can do whatever you want, it is not limited by animating sprites. – JohnEye Jul 23 '12 at 12:14
  • Can you give a code example of using BlackPawnTextureBuilder for loading atleast 3 sprites and 2 animated sprites, all of them overriding onTouch. I will be very greatful thanks. – Waqas Jul 23 '12 at 19:10
  • I will edit the answer, cannot put the code in comments as it wouldn't be formatted. – JohnEye Jul 24 '12 at 12:57
  • I think the suggestion about loading the texture atlas when loading the application is spot on. – Plastic Sturgeon Jul 24 '12 at 18:59
  • I used BuildableBitmapTextureAtlas before using simple BitmapTextureAtlas, But My animated sprite wasn't showing It was just a black picture on scene, Then I changed BuildableBitmapTextureAtlas to BitmapTextureAtlas and didn't used BlackPawnTextureBuilder and all went fine. And at the moment everything is working fine. I'm concerned about code reusability. – Waqas Jul 25 '12 at 03:57
  • But then you have to specify the position on the atlas manually, yes? Letting the builder do that for you will save a lot of effort, I think implementing it is a time well spent. Anyway, the last line of my answer is the best solution I can suggest regarding code reusability. Otherwise you would have to modify the engine to add reusable listeners to Sprites. – JohnEye Jul 26 '12 at 08:33
  • @JohnEye I will give it a try, and by the way nice solution to code reusability, Can't use this solution for my current scenario. Will use it later :). Thanks. awarding you bounty, not accepting yet may be someone can provide a better solution :). – Waqas Jul 27 '12 at 04:04
0

Are you sure you dont want more functionality than override ontouch? How about creating a class inheriting for sprite for those that need onarea to be overriden and all other needs.

Todilo
  • 1,256
  • 3
  • 19
  • 38
  • I can;t have a seperate class for every sprite. I need to give different implementation on every sprite's onTouch event. – Waqas Jul 17 '12 at 15:36
  • Then I cant think of a good way which would not instead clutter the implementation. It depends on implementation but say you have an enum of types then perhaps the onareatouch could be determined by that. All other solutions(like using some variation of the command pattern in order to pass a method as parameter)seems a bit overkill, unfortunately. I see what you are trying to do, but unfortunately, I have no solution to give. – Todilo Jul 18 '12 at 18:19