简体   繁体   English

如何从精灵表中选择特定的帧/图像?-LibGdx

[英]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. 在这里,此动画有4帧。 Atlas file look like this: Atlas文件如下所示:

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") 不要使用playAtlas.createSprites("hand")

For your case this will create 4 Spirtes and return into in Array . 对于您的情况,这将创建4个Spirtes并返回Array Sprite holds lots of data that you not required so it's only wastage of memory. Sprite拥有许多您不需要的数据,因此仅浪费内存。

so use playAtlas.findRegions("hand") instead of playAtlas.createSprites("hand") 因此请使用playAtlas.findRegions("hand")而不是playAtlas.createSprites("hand")

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

Animation is now generic so declare in this way : 现在Animation是通用的,因此可以这样声明:

Animation<TextureRegion> handAnimation;
TextureRegion handTextureRegion;

handTextureRegion = handAnimation.getKeyFrame(animationTime, true);

  1. You needed 3rd frame as Sprite or TextureRegion ? 您需要第3帧作为SpriteTextureRegion吗?

    If as Sprite use playAtlas.createSprite("hand",3); 如果作为Sprite使用playAtlas.createSprite("hand",3); or if as TextureRegion use playAtlas.findRegions("hand").get(2); 或者,如果作为TextureRegion使用playAtlas.findRegions("hand").get(2);

  2. Yes you can change order of the animation, but before Animation object initialization. 是的,您可以更改动画的顺序,但是要在Animation对象初始化之前。 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. findRegions()返回一个Array,使用该Array的元素并根据自己的自定义顺序创建自己的Array,并将新自定义的Array作为参数传递给Animation构造函数。

There is a protected method setKeyFrames(T... keyFrames) so only child of Animation can use to set new/custom KeyFrame. 有一个受保护的方法setKeyFrames(T... keyFrames)因此只有Animation的子级才能使用来设置新的/自定义的KeyFrame。

Some of playback modes are available like REVERSED , LOOP_PINGPONG , LOOP_RANDOM , but all are in some order except RANDOM . 某些播放模式可用,例如REVERSEDLOOP_PINGPONGLOOP_RANDOM ,但是除了RANDOM以外,所有播放模式都以某种顺序排列。

You can change mode of your animation any time you want by animation.setPlayMode(PlayMode playMode); 您可以随时通过animation.setPlayMode(PlayMode playMode);更改animation.setPlayMode(PlayMode playMode);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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