【问题标题】:Libgdx Animation not workingLibgdx 动画不起作用
【发布时间】:2019-01-28 06:55:06
【问题描述】:

我一直在阅读有关 libgdx 的内容,但一直被这个动画问题困扰。我从他们的 github 上阅读了 libgdx 中的动画,当我在手机上运行我的应用程序时,应用程序在加载屏幕后崩溃。这是从 Actor 扩展而来的我的玩家 Actor。我在我的菜单屏幕类中创建玩家演员并将其添加到舞台。假设在菜单屏幕中出现上下飞行,但应用程序崩溃。我知道这是我的玩家演员,因为当我在菜单屏幕类中注释掉玩家演员时,应用程序不会崩溃。我在我的平面精灵上使用纹理打包器并将其加载到我的游戏中。

public class PlayerActor extends Actor{

private DrunkPilot pGame;

private static final int NUM_ROWS = 5, NUM_COLS = 5;

private Animation<TextureRegion> drunkFlying;
private float stateTimer;
private TextureRegion region;
private Texture planeTex;

public PlayerActor(){

    planeTex = pGame.assetManager.manager.get(Constants.plane);
    planeTex = new Texture(Gdx.files.internal(Constants.plane));

    drunkFlyingAnimation();

}

private void drunkFlyingAnimation(){

    TextureRegion[][] tmp = TextureRegion.split(planeTex, planeTex.getWidth() / NUM_COLS, planeTex.getHeight() / NUM_ROWS);
    TextureRegion[] flyFrames = new TextureRegion[NUM_COLS * NUM_ROWS];

    int index = 0;
    for (int i = 0; i < NUM_ROWS; i++) {
        for (int j = 0; j < NUM_COLS; j++) {
            flyFrames[index++] = tmp[i][j];
        }
    }

    drunkFlying = new Animation<TextureRegion>(0.025f, flyFrames);
    stateTimer = 0;
    region = drunkFlying.getKeyFrame(0);
}

@Override
public void draw(Batch batch, float alpha){
    super.draw(batch, alpha);
    GdxUtils.clearScreen();
    stateTimer += Gdx.graphics.getDeltaTime();
    TextureRegion drunk = drunkFlying.getKeyFrame(stateTimer, true);

    batch.draw(drunk, getX(), getY(), getWidth() / 2, getHeight() / 2, getWidth(), getHeight(), getScaleX(), getScaleY(), getRotation());

}

public void dispose(){
    planeTex.dispose();
}

}

这是我将我的演员添加到舞台的地方。

@Override
public void show() {
    Gdx.input.setInputProcessor(menuStage);

    menuTitle = new Image(menuTitleTexture);
    menuStartImg = new Image(menuStartTexture);

    menuTable = new Table();
    menuTable.setFillParent(true);
    menuTable.add(menuTitle).pad(20).align(Align.top);
    menuTable.row();
    menuTable.add(menuStartImg).align(Align.bottom).pad(30);

    menuStage.addActor(parallaxBackground);
    menuStage.addActor(menuTable);
    menuStage.addActor(player);
}

【问题讨论】:

  • 什么异常,它是堆栈跟踪?
  • 看来pGamePlayerActor 类的空值

标签: java android libgdx


【解决方案1】:

当您使用 TexturePacker 时,有一种更简单的方法来创建动画。

当我们想要创建跑步动画时
首先,您将所有动画图像添加到 TexturePacker,您必须查看每一帧都有相同的名称 + 下划线 + frameNumber。 所以帧的名称必须是:run_1、run_2、run_3 等。

当我们创建它时,我们的资产中有一个 run.txt 和 run.png。

现在我们可以使用 AssetManager 加载 TextureAtlas:

AssetManager assetManager = new AssetManager();
assetManager.load("run.txt", TextureAtlas.class);
assetManager.finishLoading();
TextureAtlas runAnimationAtlas = assetManager.get("run.txt", TextureAtlas.class);

有了这个 TextureAtlas,我们可以创建我们的动画:

float frameDuration = 0.1f; // the duration between the animation frames
Animation<TextureRegion> runAnimation = new Animation<TextureRegion>(frameDuration, runAnimationAtlas.findRegions("run"), Animation.PlayMode.LOOP);

并渲染动画:

batch.draw(runAnimation.getKeyFrame(stateTimer),posX, posY)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多