简体   繁体   中英

How to choose a particular frame/image from sprite sheet?-LibGdx

I used to create animation like this.

private Animation handAnimation;    

handAnimation=new Animation(0.25f, playAtlas.createSprites(RegionNames.HAND_ANIMATION),Animation.PlayMode.LOOP);

drawing like this:

handTexture = handAnimation.getKeyFrame(animationTime, true);
batch.draw(handTexture, Constants.WORLD_WIDTH - (2 * handTexture.getRegionWidth()),Constants.WORLD_HEIGHT / 4);

Here this animation has 4 frames. Atlas file look like this:

hand.png
format: RGBA8888
filter: Nearest,Nearest
repeat: none
hand
  rotate: false
  xy: 1, 1
  size: 102, 152
  orig: 102, 152
  offset: 0, 0
  index: 1
hand
  rotate: false
  xy: 105, 1
  size: 102, 152
  orig: 102, 152
  offset: 0, 0
  index: 2
hand
  rotate: false
  xy: 209, 1
  size: 102, 152
  orig: 102, 152
  offset: 0, 0
  index: 3
hand
  rotate: false
  xy: 313, 1
  size: 102, 152
  orig: 102, 152
  offset: 0, 0
  index: 4

Now I want to get a particular frame (third frame) of this sprite sheet for some other use.How can I get the third frame only?

One more thing I want to know,whether it is possible to change the order of the animation with such a sprite sheet?If so how can I do it?

You can get the specific sprite from atlas

    createSprite(java.lang.String name, int index)
//Returns the first region found with the specified name and index as a sprite.

IN your code for third sprite :

playAtlas.createSprite("hand",3);

For Reverse animation try this :

handAnimation=new Animation(0.25f, playAtlas.createSprites(RegionNames.HAND_ANIMATION),Animation.PlayMode.LOOP_REVERSED);

Don't use playAtlas.createSprites("hand")

For your case this will create 4 Spirtes and return into in Array . Sprite holds lots of data that you not required so it's only wastage of memory.

so use playAtlas.findRegions("hand") instead of playAtlas.createSprites("hand")

handAnimation=new Animation(0.25f,playAtlas.findRegions("hand"), Animation.PlayMode.LOOP);

Animation is now generic so declare in this way :

Animation<TextureRegion> handAnimation;
TextureRegion handTextureRegion;

handTextureRegion = handAnimation.getKeyFrame(animationTime, true);

  1. You needed 3rd frame as Sprite or TextureRegion ?

    If as Sprite use playAtlas.createSprite("hand",3); or if as TextureRegion use playAtlas.findRegions("hand").get(2);

  2. Yes you can change order of the animation, but before Animation object initialization. findRegions() of TextureAtlas return an Array, use element of that Array and create own Array according to own custom order and pass newly custom Array as parameter in Animation constructor.

There is a protected method setKeyFrames(T... keyFrames) so only child of Animation can use to set new/custom KeyFrame.

Some of playback modes are available like REVERSED , LOOP_PINGPONG , LOOP_RANDOM , but all are in some order except RANDOM .

You can change mode of your animation any time you want by animation.setPlayMode(PlayMode playMode);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM