简体   繁体   中英

Sprites textures behave like the same texture. Java libgdx

I'm trying to make robots that are walking left or right, depends from property from Tiledmap. Walking works fine but I have issue with texture facing. Every robot's texture is facing the same as the last robot's texture, like they were using the same texture. To set texture I'm using region = Assets.instance.robotsAnimations.robotOne.getKeyFrame(timer); and every robot seams to use the same texture instead of his new own texture. How to deal with it?

That's normal because you are calling getKeyFrame method for both robots with same timer. getKeyFrame method returns the animation respected texture by timer. To solve this you should have separate timer variable for each robot and make the timer zero on moving direction change.

All Robots use the same TextureRegion.
You have static Assets class which holds one instance of RobotsAnimations and all Robots point to the same Animation in RobotsAnimations .
So when one flip the region of the Animation all other Robots use also the flipped one. Create your Animation in the Robot class so every Robot has his own Animation:

public RobotOne(PlayScreen playScreen, Rectangle rect, boolean right){
        ...

        timer = 0;
        TextureRegion[][] tmp = TextureRegion.split(Assets.instance.animationSheet,Assets.instance.animationSheet.getWidth() / cols, Assets.instance.animationSheet.getHeight() / rows);
        TextureRegion[] animationFrames = new TextureRegion[cols * rows];
        int index = 0;
        for (int i = 0; i < rows; i++){
            for (int j = 0; j < cols; j++){
                animationFrames[index++] = tmp[i][j];
            }
        }
        ownAnimation = new Animation<TextureRegion>(1/15f, animationFrames, Animation.PlayMode.LOOP)
        region = ownAnimation.getKeyFrame(timer);

        ...
    }

When you use TexturePacker there is an easier way to create animations:
Libgdx Animation not working

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