简体   繁体   中英

Libgdx Texture Region is not flipping

I had already completed drawing the texture region but when I am trying to flip the texture region is not working and even it isn't working when I am trying to draw multiple frames. I tried to check the velocity of the body inside the get Frame method but it gives me 0 when I use this code:

Gdx.app.log(String.valueOf(b2body.getLinearVelocity().x),"hi");

Can anyone help me. This is the code:

public class Collector extends Sprite {
    public enum State{STANDING,RUNNING,DEAD};
    public State currentState;
    public State previousState;
    public World world;
    public Body b2body;
    private TextureRegion collectorStand;
    private Animation collectorRun;
    private float stateTimer;
    private boolean runningRight;

    public Collector(World world,PlayScreen screen)
    {
        super(screen.getAtlas().findRegion("myboy1"));
        this.world=world;
        currentState=State.STANDING;
        previousState=State.STANDING;
        stateTimer=0;
        runningRight=true;
        Array<TextureRegion> frames=new Array<TextureRegion>();
        for(int i=0;i<3;i++)
        frames.add(new TextureRegion(getTexture(),i*90,122,90,300));
        //frames.add(new TextureRegion(getTexture(),90,122,110,300));
        //frames.add(new TextureRegion(getTexture(),200,122,110,300));

        collectorRun=new Animation(0.1f,frames);
        frames.clear();


        collectorStand=new TextureRegion(getTexture(), 0, 122, 89, 300);
        //collectorStand=new TextureRegion(getTexture(),90,122,110,300);
        //collectorStand=new TextureRegion(getTexture(),200,122,110,300);
        defineCollector();
        setBounds(0,0,50/Fruits.PPM,100/Fruits.PPM);//here we can change the size of our Animation


        setRegion(collectorStand);
    }
    public TextureRegion myregion(float dt)
    {
        TextureRegion region;
        region=collectorStand;

        Gdx.app.log(String.valueOf(b2body.getLinearVelocity().x),"hi");
        if(b2body.getLinearVelocity().x<0 )
        {
            region.flip(true,false);
        }

        return region;
    }
    public void update(float dt)
    {
        setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - getHeight() / 2.8f);
        //setRegion(myregion(dt));
        setRegion(getFrame(dt));
    }
    public TextureRegion getFrame(float dt)// return the appropriate frames for the sprite to be drawn
    {
        currentState=getState();
        TextureRegion region;
        switch (currentState)
        {
            case RUNNING:
                region=collectorRun.getKeyFrame(stateTimer,true);
                break;
            case STANDING:
            default:
                region=collectorStand;
                break;
        }
        Gdx.app.log(String.valueOf(b2body.getLinearVelocity().x),"hi");
        if((b2body.getLinearVelocity().x<0 || !runningRight)&& !region.isFlipX())
        {
            region.flip(true,false);
            runningRight=false;
        }
        else if((b2body.getLinearVelocity().x>0 || runningRight)&& region.isFlipX())
        {
            region.flip(true,false);
            runningRight=true;
        }
        stateTimer=currentState==previousState?stateTimer+dt:0;
        previousState=currentState;
        return region;
    }
    public State getState()
    {
        if(b2body.getLinearVelocity().x!=0)
            return State.RUNNING;
        else
            return State.STANDING;
    }
    public void defineCollector()
    {
        BodyDef bdef=new BodyDef();
        bdef.position.set(72/Fruits.PPM,32/Fruits.PPM);
        bdef.type=BodyDef.BodyType.DynamicBody;
        b2body=world.createBody(bdef);
        FixtureDef fdef=new FixtureDef();
        //PolygonShape shape=new PolygonShape();
        CircleShape shape=new CircleShape();
        shape.setRadius(20/Fruits.PPM);
        fdef.shape=shape;
        b2body.createFixture(fdef);
    }
}

Flipping using TextureRegion.flip() is not a good approach for constant flipping of images. The only time you should be doing this is when you load the image and it is only being done once because it is fairly computationally expensive since it actually modifies the image data to flip it rather than just draw it flipped.

You should instead flip the image when calling batch.draw() . Here is a guide on how to use batch calls with more parameters: libgdx: Rotate a texture when drawing it with spritebatch I'm actually the question poster :P

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