[英]Libgdx Texture Region is not flipping
我已经完成了纹理区域的绘制,但是当我尝试翻转纹理区域时,即使试图绘制多个框架,它也无法正常工作。 我试图在get Frame方法中检查物体的速度,但是当我使用以下代码时,它给了我0:
Gdx.app.log(String.valueOf(b2body.getLinearVelocity().x),"hi");
谁能帮我。 这是代码:
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);
}
}
使用TextureRegion.flip()
进行翻转不是持续翻转图像的好方法。 唯一应该做的就是加载图像,并且只执行一次,因为它在计算上相当昂贵,因为它实际上修改了图像数据以翻转它,而不是仅仅翻转它。
您应该在调用batch.draw()
时翻转图像。 这是有关如何使用带有更多参数的批处理调用的指南: libgdx:使用spritebatch绘制纹理时旋转纹理我实际上是问题发帖人:P
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.