我已经完成了绘制纹理区域,但是当我试图翻转纹理区域时,它不起作用,甚至当我尝试绘制多个帧时它也不起作用。我尝试在get框架方法中检查主体的速度,但是当我使用此代码时它给出了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);
}
}
答案 0 :(得分:0)
使用TextureRegion.flip()
翻转并不是一种不断翻转图像的好方法。你应该这样做的唯一一次是当你加载图像并且它只进行一次因为它的计算成本相当昂贵,因为它实际上修改了图像数据以翻转它而不是仅仅翻转它。
您应该在调用batch.draw()
时翻转图像。以下是如何使用包含更多参数的批量调用的指南:libgdx: Rotate a texture when drawing it with spritebatch我实际上是问题海报:P