gpt4 book ai didi

java - libGDX Sprite 表 2d 动画

转载 作者:行者123 更新时间:2023-12-02 13:20:21 27 4
gpt4 key购买 nike

我正在尝试使用 libGDX 在 Java 中创建简单的 2d 游戏。我坚持从 PNG 文件中的 Sprite 表创建 2D 动画。

我有 AbstractScreen 类,它为每个屏幕保存“东西”。

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

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

首先我创建了AbstractScreen和MainMenuScreen(基于AbstractScreen)。它有效,但现在我正在尝试用我的动画播放器创建 GameScreen 。出了点问题,因为我没有任何错误,并且播放器在应用程序屏幕上不可见。如何为我的应用程序的骨架正确实现 Sprite 表动画?

我的游戏画面:

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));
}
}

最佳答案

在您的 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());
}
}

编辑

您需要根据设备屏幕的宽度和高度调整舞台视口(viewport)的大小,因此重写resize方法,也不需要调用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);
}

关于java - libGDX Sprite 表 2d 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43590626/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com