[英]Multiple instances of same sprite class showing different animation frames in libGDX and box2d
所以这是我的问题。 我创建了一个敌人类,该类在libGDX中扩展了Sprite,并在其中带有Box2d主体以进行碰撞检测。 我的代码在循环中在游戏中动态地调用敌人类别,但是问题是,在碰撞后,我使用不同的动画序列来描绘被击败的敌人时,该动画序列针对同一类别的不同实例显示了不同的帧频。 我不知道为什么会这样。
这是在循环中调用的类。
public class Spearman extends Enemies {
private Body spearman1;
private TextureAtlas atlas;
private Animation approaching,defeated;
private float time;
private TextureRegion spearmaninit;
private int spearmanstate=0;
public Spearman(Play_State state,float x, float y){
super(state,x,y);
atlas=new TextureAtlas();
atlas=Lone_Warrior1.getAtlas(3);
time=0f;
Array<TextureRegion> frames=new Array<TextureRegion>();
for(int i=0;i<3;i++)
{
frames.add(new TextureRegion(atlas.findRegion("Spearman"+i)));
}
approaching=new Animation(0.15f,frames);
frames.clear();
for(int i=0;i<5;i++)
{
frames.add(new TextureRegion(atlas.findRegion("Spearmandefeated"+i)));
}
defeated=new Animation(2.2f,frames);
frames.clear();
spearmaninit=new TextureRegion(atlas.findRegion("Spearman0"));
setBounds(getX(),getY(),200/ Lone_Warrior1.PPM,170/Lone_Warrior1.PPM);
setRegion(spearmaninit);
}
public void update(float dt){
if(spearmanstate!=-1) {
setPosition(spearman1.getPosition().x - getWidth() / 2, (spearman1.getPosition().y - getHeight() / 2)+13/Lone_Warrior1.PPM);
if (spearmanstate==0 && spearman1.getLinearVelocity().x>0)
spearmanstate=1;
if (spearmanstate==1 && spearman1.getLinearVelocity().x==0) {
Play_State.bodiesToRemove.add(spearman1);
Play_State.enemycounter++;
spearmanstate = -1;
}
if(spearmanstate==0)
spearman1.setLinearVelocity(-2f,0);
setRegion(getFrame(dt));
}
}
public boolean check(){
if(spearmanstate!=-1)
return true;
else
return false;
}
public TextureRegion getFrame(float dt){
TextureRegion region=null;
region=approaching.getKeyFrame(time,true);
if(spearmanstate==1)
region=defeated.getKeyFrame(time);
/* if(!region.isFlipX())
region.flip(true,false);*/
time=time+dt;
return region;
}
@Override
public void defineEnemy() {
BodyDef bdef=new BodyDef();
bdef.position.set(getX(),getY());
bdef.type=BodyDef.BodyType.DynamicBody;
spearman1=Play_State.world.createBody(bdef);
FixtureDef fdef=new FixtureDef();
PolygonShape War1=new PolygonShape();
War1.setAsBox(60/Lone_Warrior1.PPM,60/Lone_Warrior1.PPM);
fdef.shape=War1;
fdef.filter.categoryBits=Lone_Warrior1.BIT_APPROACHING;
fdef.filter.maskBits=Lone_Warrior1.BIT_GROUND|Lone_Warrior1.BIT_RUN|Lone_Warrior 1.BIT_ATTACK;
spearman1.createFixture(fdef).setUserData("spearman1");
}
}
调用Spearman的抽象类:
public abstract class Enemies extends Sprite {
World world;
Body b2body;
public Enemies(){
}
public Enemies(Play_State screen,float x, float y){
this.world=screen.world;
setPosition(x,y);
defineEnemy();
}
public abstract void defineEnemy();
public abstract void update(float dt);
public abstract boolean check();
}
这是调用该类的代码。
switch (a) {
case 1:
System.out.println("creating spearman");
spearman.add(new Spearman(screen, (Lone_Warrior1.V_Width /Lone_Warrior1.PPM) + (Lone_Warrior1.x+``(i*500/Lone_Warrior1.PPM)), 100 / Lone_Warrior1.PPM));
break;
}
解决了!
是的,Sprite类对于逐帧动画有点麻烦,但是我已经完成了很多代码,因此需要为该问题找到补丁作业,而不必重写整个代码。
所以这是答案:
time=currentState==previousState ? time+dt : 0;
previousState=currentState;
每当您在update方法中使用增量时间更新时间时,请确保检查当前动画序列是否等于上一个动画序列;如果不等于,则将时间设为零,然后在碰撞后,下一个动画将按预期的序列播放。只需为您所处的状态放置一个跟踪器即可。
int State=1://the default form for player
State=2;//the state of animation when the player is hit`
` currentState=State;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.