1

I have 2 objects of Animation class in my Renderer class that takes 2 files of animations of a monkey : one depicts the monkey going forward and the other one to the right side.

Now, I'm trying to create a third object of animation that will draw the monkey going left.

I'm trying to flip the images of the second Animation( monkey goes right) and add them to an array that will eventually be used as a resource for the third object.

The problem is when I flip the images of the second animation and then add them, the original animation object is "damaged". When I say damaged, I mean the Animation that contains the images of the monkey going right is suddently also "flipped" to the left and now I have 2 Animation objects that describe the monkey going to the left.

Here is the part of the code that deals with it:

monkeyAnimStraight = new Animation(1/20.0f, AssetsManager.getAsset("animations/monkeywalkaway.txt", TextureAtlas.class).getRegions());
        monkeyAnimStraight.setPlayMode(Animation.PlayMode.LOOP_REVERSED);
        monkeyAnimRight = new Animation(1/10.0f, AssetsManager.getAsset("animations/monkeywalk.txt", TextureAtlas.class).getRegions());
        monkeyAnimRight.setPlayMode(Animation.PlayMode.LOOP_REVERSED);
        Array<AtlasRegion> temp = AssetsManager.getAsset("animations/monkeywalk.txt", TextureAtlas.class).getRegions();
        Array<AtlasRegion> keyFramesTemp = new Array<TextureAtlas.AtlasRegion>();
        keyFramesTemp.addAll(temp);
        for(AtlasRegion keyFrame : keyFramesTemp) {
            keyFrame.flip(true, false);
        }
        monkeyAnimLeft = new Animation(1/20.0f, keyFramesTemp);

As you can see, I'm trying to copy the original images so they won't be flipped but it still doesn't work.

How can I successfully copy the original AtlasRegion array?

4

1 回答 1

3

如果您不想弄乱原始纹理区域,请创建新纹理区域。请注意,AtlasRegions 是 TextureRegions 的子类,但您的动画真正需要的是 TextureRegions。

无需修改 TextureRegions 的原始实例,您需要复制它们并创建新实例。简单地将它们放到一个新数组中并不会复制它们。新数组仍然引用相同的原始实例。

所以这样做:

Array<TextureRegion> monkeyWalkRightRegions = AssetsManager.getAsset("animations/monkeywalk.txt", TextureAtlas.class).getRegions();
monkeyAnimRight = new Animation(1/10.0f, monkeyWalkRightRegions);
monkeyAnimRight.setPlayMode(Animation.PlayMode.LOOP_REVERSED);

Array<TextureRegion> monkeyWalkLeftRegions = new Array<TextureRegion>();

//copy and then flip each region and add it to the new array
for(TextureRegion keyFrame : monkeyWalkRightRegions ) {
    TextureRegion region = new TextureRegion(keyFrame); //copy of region created
    region.flip(true, false);
    monkeyWalkLeftRegions.add(region);
}

monkeyAnimLeft = new Animation(1/20.0f, monkeyWalkLeftRegions );
于 2014-10-25T01:09:40.117 回答