0
\$\begingroup\$

I have multiple sprite sheets for my object(Parrot) like eating, moving left to right, right to left and much more. I am using AndEngine gles2.0. How should I implement it? Every time I have to use different sprite sheet, detach previous one and on same location add new one. This is going to be hectic!

Anyone here who already implemented this kind of scenario? If I use single sprite sheet for every animation it size will increase and it will throw a exception of out of memory!

\$\endgroup\$

3 Answers 3

1
\$\begingroup\$

The only reason you would want to have a "left to right" and vice versa animaton is if they are actually different. If this is not the case you simply flip your sprite on the y axis to -1 and back to 1 depending on the direction your sprite is walking in. If he is normally facing right, -1 will make him face left.

Usually a full character animation fits onto one sprite sheet.
Try TexturePacker ( semi-free)
or
libgdxs texturepacker (free)

The files these packers generate could surely be used in AndEngine. I also recommand looking at LibGDX for gamedevelopment on Android. It takes a bit more effort to setup but in the end it gives you far more freedom.

If I use single sprite sheet for every animation it size will increase and it will throw a exception of virtual memory exceed!

You need to optimize your TextureAtlas/SpriteSheet. Like I said, keep the animations for both directions to a single direction and just flip your sprite. You can also reduce the quality of your sheet to a point you won't notice and save up on memory use.

Edit: As to your comment below: You simply need to bind/swap textures then. I suggest creating a class with animation properties ( like the amount of frames, framerate, rectangle size, etc) that also holds the sprite sheet texture. Then you have animation player class that holds all of these animation objects. This is just an example and not the absolute way ( I don't know about the build in sprite sheet application of AndEngine, just throwing a bone here):

class SingleRowSpriteSheet{
  private MySpriteSheetTexture sheet;
  private Rect rect;
  private int frameRate;
  private int frames;      
  private currentFrame;

  public MySpriteSheetTexture getSheet(){return sheet;);
  public drawOnTo(MyTextureTarget target); // Draws on your texture, also binds the texture if it's not already done.
  public update(); // update currentFrame and looping
}

\

 import java.util.*;
class SingleRowAnimator{
   private MyTextureTarget target; // don't know the texture class for AndEngine. but it's the reference to the texture of your sprite;

   private SingleRowSpriteSheet currentAnimation;
   private HashMap map;
   //Using a hash map so that you can call animations by name
   public void animateTo(MyTextureTarget texture);
   public void addAnimation(String name, SingleRowSpriteSheet srss){
      map.put(name,srss)
   };


   public void playAnimation(String name, int atFrame){
     currentAnimation = map.get(name);
   };

   public void update(){

          // do update stuff here
         currentAnimation.update();
         currentAnimation.drawOnTo(target) // draws onto your target.

   }
   etc.
}

SingleRowAnimator would be an aggregated object put in your entity object. Then when you need to change animation simply call: playAnimation;

This is just a rough idea of how you COULD do it. I hope this helps.

edit: Just noticed that these 2 class names are a bit misleading. But just look at the idea.

edit: What I don't get is, even for this, you would need to load every SpriteSheet into your game. So to my idea this would also cause a memory error. Make sure you are working with textures that are sized for your target resolution. Use bilinear/trilinear filtering to smooth out the pixels.

As an alternative to my solution, see if there is a way to copy pixels from a spritesheet unto your texture target.

\$\endgroup\$
3
  • \$\begingroup\$ Right mow I am interested in using multiple sprite sheets for single object, tried single sheet and it gave memory error. Will look into optimizing the single sprite sheet If in changing sheets I got any smoothness issue. Thanks \$\endgroup\$
    – Waqas
    Aug 6, 2012 at 9:37
  • \$\begingroup\$ @waqas716 I've updated my answer \$\endgroup\$
    – Sidar
    Aug 6, 2012 at 22:34
  • \$\begingroup\$ Thanks for your help, I have done a way around will upload it shortly, You can have a look and tell out of memory will happen or not. \$\endgroup\$
    – Waqas
    Aug 7, 2012 at 4:41
1
\$\begingroup\$

This is my code to perform animations of different sprite sheets.

public void performEatingAnimation(Bird b) {
    float x = b.getSelectedAnimatedsprite().getX();
    float y = b.getSelectedAnimatedsprite().getY();
    b.getSelectedAnimatedsprite().setVisible(false);

    b.getSelectedAnimatedsprite().detachSelf();
    b.setSelectedAnimatedsprite(b.getEatingAnimatedsprite());

    attachObjectWithNewPosition(b.getSelectedAnimatedsprite(), x, y);
    b.getSelectedAnimatedsprite().animate(60, 2);

}

public void performWalkAnimation(Bird b) {
    float x = b.getSelectedAnimatedsprite().getX();
    float y = b.getSelectedAnimatedsprite().getY();
    b.getSelectedAnimatedsprite().setVisible(false);

    b.getSelectedAnimatedsprite().detachSelf();
    b.setSelectedAnimatedsprite(b.getWalkingAnimatedsprite());

    attachObjectWithNewPosition(b.getSelectedAnimatedsprite(), x, y);
    b.getSelectedAnimatedsprite().animate(60, 0)
 }

And here is my setSelected method to perform a deepCopy of AnimatedSprite.

public void setSelectedAnimatedsprite(AnimatedSprite selectedAnimatedsprite) {
    AnimatedSprite sprite = new AnimatedSprite(
            selectedAnimatedsprite.getX(), selectedAnimatedsprite.getY(),
            selectedAnimatedsprite.getTiledTextureRegion().deepCopy(),
            selectedAnimatedsprite.getVertexBufferObjectManager());

    this.selectedAnimatedsprite = sprite;
}
\$\endgroup\$
3
  • \$\begingroup\$ @Sidar what do you say about it? and yes everything is working fine right now. No glitches in animation etc. \$\endgroup\$
    – Waqas
    Aug 7, 2012 at 5:07
  • \$\begingroup\$ If that works keep it. However I wouldn't limit the parameter to a "Bird". Make an interface instead so that other objects can be animated by the same function call. Unless bird is the only thing that is going to be animated. Also is the new really necessary? Can't you just pass the selectedAnimatedsprite parameter? Ideally I would rather do something like mySprite.playMation("walk"); But if your game is simplistic don't worry about it too much. Manage your textures at one place and reference to them. Don't make new ones. If you create new Textures for every game object you will kill your ram. \$\endgroup\$
    – Sidar
    Aug 7, 2012 at 13:46
  • \$\begingroup\$ Right now it will only a bird who is going to animate, and yes I have to new the selected sprite because if I don't do that it will pass the reference and setVisible(false) and detach will happen on WalkingAnimatedSprite and EatingAnimatedSprite which I don't want in anycase, By the way nice suggestion of mySprite.playMation("walk"); will do that. \$\endgroup\$
    – Waqas
    Aug 8, 2012 at 5:00
0
\$\begingroup\$

As of reading my own comment. It could very well be that you create new textures for every object in your game.

Always have one pool of textures / One place the texture is read from and let your sprites reference/point to it.

It simply sounds impossible for ONE spritesheet to make your device go out of memory. Most games today certainly have at least 2 to n spritesheets.

\$\endgroup\$
4
  • \$\begingroup\$ I had one sprite sheet containing 80 rows and 4 cols. Size was 4mb. \$\endgroup\$
    – Waqas
    Aug 8, 2012 at 5:01
  • \$\begingroup\$ Like I mentioned before, you should take out duplicate animations for each direction. If they are the same just flip your sprite. Also 4 mb is pretty high, you could definitely reduce the size. Could you provide a link to your spritesheet? \$\endgroup\$
    – Sidar
    Aug 8, 2012 at 22:58
  • \$\begingroup\$ That sheet was for testing purpose, we have to animate the bird in so many ways i.e minimum 20 different animations of single object. If one animation takes 4 rows and 4cols it is going to be 80 rows right :). \$\endgroup\$
    – Waqas
    Aug 9, 2012 at 4:29
  • \$\begingroup\$ No im not talking about the resolution, I'm talking about the file size. \$\endgroup\$
    – Sidar
    Aug 9, 2012 at 5:11

You must log in to answer this question.

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