同一精灵 class 的多个实例在 libGDX 和 box2d 中显示不同的动画帧
Multiple instances of same sprite class showing different animation frames in libGDX and box2d
所以这是我的问题。我创建了一个敌人 class,它在 libGDX 中扩展了 Sprite,其中包含一个用于碰撞检测的 Box2d 实体。我的代码在游戏中循环动态调用敌人 class,但问题是当碰撞后我使用不同的动画序列来描绘被击败的敌人时,动画序列为不同的实例显示不同的帧速率相同 class。我不知道为什么会这样。
这里是循环调用的class
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");
}
}
召唤矛兵的摘要class:
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();
}
这是调用 class 的代码。
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 class 对于逐帧动画来说有点麻烦,但我已经完成了大部分代码,需要为这个问题找到一个补丁作业,而不是重写整个代码.
答案如下:
time=currentState==previousState ? time+dt : 0;
previousState=currentState;
每当您在更新方法中使用增量时间更新时间时,请务必检查当前动画序列是否与前一个动画序列相同,如果不相同则将时间设为零,然后在碰撞后播放下一个动画在预期的 sequence.Just 中为您所在的州放置一个跟踪器。
int State=1://the default form for player
State=2;//the state of animation when the player is hit`
` currentState=State;
所以这是我的问题。我创建了一个敌人 class,它在 libGDX 中扩展了 Sprite,其中包含一个用于碰撞检测的 Box2d 实体。我的代码在游戏中循环动态调用敌人 class,但问题是当碰撞后我使用不同的动画序列来描绘被击败的敌人时,动画序列为不同的实例显示不同的帧速率相同 class。我不知道为什么会这样。
这里是循环调用的class
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");
}
}
召唤矛兵的摘要class:
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();
}
这是调用 class 的代码。
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 class 对于逐帧动画来说有点麻烦,但我已经完成了大部分代码,需要为这个问题找到一个补丁作业,而不是重写整个代码.
答案如下:
time=currentState==previousState ? time+dt : 0;
previousState=currentState;
每当您在更新方法中使用增量时间更新时间时,请务必检查当前动画序列是否与前一个动画序列相同,如果不相同则将时间设为零,然后在碰撞后播放下一个动画在预期的 sequence.Just 中为您所在的州放置一个跟踪器。
int State=1://the default form for player
State=2;//the state of animation when the player is hit`
` currentState=State;