libGDX精灵表2d动画

时间:2017-04-24 14:20:30

标签: java android animation libgdx 2d

我试图用libGDX在Java中创建简单的2D游戏。 我坚持在PNG文件中从精灵表创建2d动画。

我有AbstractScreen类来保存'事物'对于每个屏幕。

用于绘制我的游戏的GameScreen类和带有动画代码的Player类。

玩家类来自https://github.com/libgdx/libgdx/wiki/2D-Animation示例。

首先我创建了AbstractScreen和MainMenuScreen(基于AbstractScreen)。它的工作原理,但现在我正试图用我的动画播放器创建GameScreen。有些事情是错误的,因为我没有任何错误,玩家在应用程序屏幕上看不到。如何正确实现我的应用程序骨架的精灵表动画?

我的游戏画面:

class GameScreen extends AbstractScreen {
Player player;

public GameScreen(NinjaGame game) {
    super(game);
    init();
}

@Override
protected void init() {
    initPlayer();
}

private void initPlayer() {
    player = new Player();
    player.setDebug(true);
    stage.addActor(player);
}

@Override
public void render(float delta) {
    super.render(delta);
    update();
    spriteBatch.begin();
    stage.draw();
    spriteBatch.end();
}

private void update() {
    stage.act();
    }
}

我的AbstractScreen类:

public abstract class AbstractScreen implements Screen {
protected NinjaGame game;
protected Stage stage;
private OrthographicCamera camera;
protected SpriteBatch spriteBatch;

public AbstractScreen(NinjaGame game) {
    this.game = game;
    createCamera();
    /* Stage for actors */
    stage = new Stage(new StretchViewport(NinjaGame.SCREEN_WIDTH, NinjaGame.SCREEN_HEIGHT, camera));
    /* Batch for sprites */
    spriteBatch = new SpriteBatch();
    /* Stage takes user inputs */
    Gdx.input.setInputProcessor(stage);
    init();
}

protected abstract void init();

private void createCamera() {
    /* Orthographic means like in CAD drawings */
    camera = new OrthographicCamera();
    camera.setToOrtho(false, NinjaGame.SCREEN_WIDTH, NinjaGame.SCREEN_HEIGHT);
    camera.update();
}

/** Clean screen on black color between render frames */
private void clearScreen() {
    Gdx.gl.glClearColor(0, 0, 0, 0);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}

@Override
public void render(float delta) {
    clearScreen();
    camera.update();
    spriteBatch.setProjectionMatrix(camera.combined);
    }

}

我的玩家类:

public class Player extends Actor {
private static final int FRAME_COLS = 10, FRAME_ROWS = 1;
Animation<TextureRegion> walkAnimation;
Texture walkSheet;
private final static int STARTING_X = 50;
private final static int STARTING_Y = 50;

public Player(){
    createIdleAnimation();
    this.setPosition(STARTING_X, STARTING_Y);
}

private void createIdleAnimation() {
    walkSheet = new Texture(Gdx.files.internal("sheets/ninja-idle-sheet.png"));

    TextureRegion[][] tmp = TextureRegion.split(walkSheet,
            walkSheet.getWidth() / FRAME_COLS,
            walkSheet.getHeight() / FRAME_ROWS);

    TextureRegion[] walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
    int index = 0;
    for (int i = 0; i < FRAME_ROWS; i++) {
        for (int j = 0; j < FRAME_COLS; j++) {
            walkFrames[index++] = tmp[i][j];
        }
    }

    walkAnimation = new Animation<TextureRegion>(0.025f, walkFrames);
}

@Override
public void act(float delta) {
    super.act(delta);
}

@Override
public void draw(Batch batch, float parentAlpha) {
    super.draw(batch, parentAlpha);
    }
}

更新1

NinjaGame课程

public class NinjaGame extends Game{
public final static int SCREEN_WIDTH = 800;
public final static int SCREEN_HEIGHT = 480;
public final static String GAME_NAME = "ninja vs zombie";
private boolean paused;

@Override
public void create() {
    this.setScreen(new GameScreen(this));
}
}

1 个答案:

答案 0 :(得分:1)

在您的Player类中进行小修改,以便绘制播放器动画帧。你需要重载Actor的draw方法并绘制Player的纹理。

public class Player extends Actor {
    private static final int FRAME_COLS = 10, FRAME_ROWS = 1;
    Animation<TextureRegion> walkAnimation;
    Texture walkSheet;
    private final static int STARTING_X = 50;
    private final static int STARTING_Y = 50;
    TextureRegion reg;
    float stateTime;

    public Player(){
        createIdleAnimation();
        this.setPosition(STARTING_X, STARTING_Y);
    }

    private void createIdleAnimation() {
        walkSheet = new Texture(Gdx.files.internal("sheets/ninja-idle-sheet.png"));

        TextureRegion[][] tmp = TextureRegion.split(walkSheet,
                walkSheet.getWidth() / FRAME_COLS,
                walkSheet.getHeight() / FRAME_ROWS);

        TextureRegion[] walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
        int index = 0;
        for (int i = 0; i < FRAME_ROWS; i++) {
            for (int j = 0; j < FRAME_COLS; j++) {
                walkFrames[index++] = tmp[i][j];
            }
        }

        walkAnimation = new Animation<TextureRegion>(0.025f, walkFrames);
        stateTime = 0f;
        reg=walkAnimation.getKeyFrame(0);
    }

    @Override
    public void act(float delta) {
        super.act(delta);

        stateTime += delta;
        reg = walkAnimation.getKeyFrame(stateTime,true);
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);

        Color color = getColor();
        batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
        batch.draw(reg,getX(),getY(),getWidth()/2,getHeight()/2,getWidth(),getHeight(),getScaleX(),getScaleY(),getRotation());
    }
}

修改

您需要根据设备屏幕宽度和高度调整舞台视口的大小,以覆盖resize方法,也无需调用spritebatch的beginend。你正在使用拥有SpriteBatch

的舞台
class GameScreen extends AbstractScreen {

  Player player;

  public GameScreen(NinjaGame game) {
    super(game);
    init();
  }

  @Override
  protected void init() {
    initPlayer();
  }

  private void initPlayer() {
    player = new Player();
    player.setDebug(true);
    stage.addActor(player);
  }

  @Override
  public void render(float delta) {
    super.render(delta);

    update();
    stage.draw();
  }

  private void update() {
    stage.act();
  }

  @Override
  public void resize(int width, int height){
    stage.getViewport().update(width,height);
  }
}

编辑2

private void initPlayer() {
     player = new Player();
     player.setSize(100,150);
     player.setDebug(true);
     stage.addActor(player);
}