【发布时间】:2016-11-21 13:03:28
【问题描述】:
我的movement animation 没有播放。我的角色仍然摆姿势表演,但在移动时它保持在那个静态位置。 animation loop 似乎没有播放。
我的代码如下:
public class Char extends Sprite {
public enum State { STANDING, RUNNING };
public State currentState;
public State previousState;
TextureRegion region;
private TextureRegion Still;
private Animation Go;
private float stateTimer;
public World world;
public Body b2body;
public Char(World world, PlayScreen screen){
super(screen.getAtlas().findRegion("NewRun"));
currentState = State.STANDING;
previousState = State.STANDING;
stateTimer = 0;
Array<TextureRegion> frames = new Array<TextureRegion>();
for(int i = 1; i < 4; i++)
frames.add(new TextureRegion(getTexture(),i* 53,0,53,48));
Go = new Animation(0.1f, frames);
frames.clear();
Still = new TextureRegion(getTexture(),0,0,53,50);
setBounds(0, 0, 50 / Game.PPM, 65 / Game.PPM);
setRegion(Still);
this.world = world;
defineChar();
}
public void update(float dt) {
setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - getHeight() / 2);
setRegion(getFrame(dt));
}
public TextureRegion getFrame(float dt) {
currentState = getState();
switch(currentState){
case RUNNING:
Go.getKeyFrame(stateTimer, true);
break;
case STANDING:
default:
region = Still;
break;
}
stateTimer = currentState == previousState ? stateTimer + dt : 0;
//update previous state
previousState = currentState;
//return our final adjusted frame
return region;
}
public void defineChar(){
BodyDef bdef = new BodyDef();
bdef.position.set(50/ JoD.PPM, 20/ JoD.PPM);
bdef.type =BodyDef.BodyType.DynamicBody;
b2body = world.createBody(bdef);
FixtureDef fdef = new FixtureDef();
CircleShape shape =new CircleShape();
shape.setRadius(4);
fdef.shape= shape;
b2body.createFixture(fdef);//.setUserData(this);
}
public State getState(){
if(b2body.getLinearVelocity().x != 0)
return State.RUNNING;
//if none of these return then he must be standing
else
return State.STANDING;
}
}
我查看了多个教程,但不明白为什么动画无法播放。
谁能看到我错过了什么?
【问题讨论】:
-
你能保证 Sprite 正在接收更新事件吗?您是否明确将状态设置为 RUNNING,动画是否有效?
标签: java android animation libgdx