java - Libgdx Texture Region is not flipping -
i had completed drawing texture region when trying flip texture region not working , isn't working when trying draw multiple frames. tried check velocity of body inside frame method gives me 0 when use code:
gdx.app.log(string.valueof(b2body.getlinearvelocity().x),"hi");
can me. 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 can change 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 appropriate frames sprite 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()
not approach constant flipping of images. time should doing when load image , being done once because computationally expensive since modifies image data flip rather draw flipped.
you should instead flip image when calling batch.draw()
. here guide on how use batch calls more parameters: libgdx: rotate texture when drawing spritebatch i'm question poster :p