所以这是我的问题。我创建了一个敌人类,它在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;
}
答案 0 :(得分:0)
解决!
是的,Sprite类对于逐帧动画来说有点麻烦,但我已经完成了很多代码,需要为这个问题找到一个补丁作业,而不必重写整个代码。
所以这就是答案:
time=currentState==previousState ? time+dt : 0;
previousState=currentState;
每当你在更新方法中使用增量时间更新时间时,请确保检查当前动画序列是否等于前一个动画序列,如果不是,则将时间设为零,然后在碰撞后,下一个动画将在预定的顺序。只需为你所在的国家设置一个跟踪器。
int State=1://the default form for player
State=2;//the state of animation when the player is hit`
` currentState=State;