简体   繁体   中英

Copy texture in libgdx

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?

If you don't want to mess up the original texture regions, then create new ones. Note that AtlasRegions are subclasses of TextureRegions, but all you really need for your Animation are TextureRegions.

Instead of modifying the original instances of your TextureRegions, you need to copy them and create new ones. Simply dropping them into a new array doesn't copy them. The new array is still referencing the same original instances.

So do this:

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 );

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