我正在学习在大学里做游戏。
我有一个小问题,我需要帮助为我的游戏(弹跳经典游戏)设置2D动画。
我不知道如何加载和使用TextureRegion[][]
,因为对于普通动画(由4 png制作),效果很好,但是对png矩阵不起作用
这是我的课堂动画
public abstract class Animations extends Sprite {
protected TextureAtlas atlas = new TextureAtlas("sprites/Animation_light_Ball.atlas");
protected Body body;
protected TextureRegion ballStand;
protected Array<TextureRegion> frames;
protected float radius = 42;
protected float stateTimer;
private static final int BOUNDS_WIDTH = 90;
private static final int BOUNDS_HEIGHT = 90;
public Animations() {
}
public Animations(final String nameRegion) {
setRegion(new TextureRegion(atlas.findRegion(nameRegion),0,0,120,120));
setBounds(0,0,BOUNDS_WIDTH,BOUNDS_HEIGHT);
}
public Animation setAnimation(final int startFrame, final int endFrame, final String animationName,
final int height, final int width) {
final float duration = 0.12f;
frames = new Array<>();
for (int i = startFrame; i < endFrame; i++) {
frames.add(new TextureRegion(atlas.findRegion(animationName), i * 120, 0, height, width));
}
return new Animation(duration, frames);
}
public void cleanFrames() {
frames.clear();
}
public void dispose() {
atlas.dispose();
}
public void draw(final Batch batch) {
super.draw(batch);
}
protected abstract void update(float dt);
}
这是我的课程ballViewImpl
public class BallViewImpl extends Animations implements BallView {
public World world;
public Body body;
private static final int BOUNDS_WIDTH = 90;
private static final int BOUNDS_HEIGHT = 90;
public State currentState;
public State previousState;
private TextureRegion ballStand;
private Animation rollingBall;
private float stateTimer;
private Ball ball;
private boolean rollingRight;
public BallViewImpl() {
rollingRight= false;
ballStand = new TextureRegion(atlas.findRegion("Animation_light_ball-1"),0,0,120,120);
rollingBall = setAnimation(0,4,"Animation_light_ball-1",120,120);
setBounds(0,0,BOUNDS_WIDTH,BOUNDS_HEIGHT);
}
private TextureRegion setSprite(final float delta) {
TextureRegion region = new TextureRegion();
region = getFrame(delta);
if ((body.getLinearVelocity().x < 0 || !rollingRight) && !region.isFlipX()) {
region.flip(true, false);
rollingRight = false;
} else if (rollingRight && region.isFlipX()) {
region.flip(true, false);
rollingRight = true;
}
return region;
}
@Override
public void setBall(Ball ball) {
this.ball = ball;
this.body = ball.getBody();
}
@Override
public float getStateTimer() {
return stateTimer;
}
@Override
public void update(float dt) {
rollingRight = ball.isRolling();
setPosition(body.getPosition().x - getWidth()/2, body.getPosition().y - getHeight()/2);
setRegion(setSprite(dt));
}
private TextureRegion getFrame(float dt) {
previousState = currentState;
currentState = ball.getState(); //ballimplement
stateTimer = currentState == previousState ? stateTimer +dt : 0;
TextureRegion region = new TextureRegion();
switch(currentState) {
case ROLLING:
region = rollingBall.getKeyFrame(stateTimer,true);
break;
case FALLING:
case STANDING:
default:
region = ballStand;
break;
}
return region;
}
@Override
public void draw(final Batch batch) {
super.draw(batch);
}
}