【问题标题】:LibGDX - Trying to animate a sprite, crashes my whole gameLibGDX - 试图为精灵设置动画,导致我的整个游戏崩溃
【发布时间】:2015-02-14 04:58:22
【问题描述】:

我正在开发一款 Android 游戏,到目前为止,我绘制游戏对象的方式是在 Game 中初始化它们,然后将它们放入 GameObject 的数组列表中em>(每个对象都扩展了这个抽象类;玩家、旗帜、硬币)。这个数组列表被传递给 Renderer,然后使用 for 循环(迭代数组列表)绘制对象。

我正在尝试添加一个名为 Coin 的新 GameObject。与其他对象不同,我希望这个对象具有动画效果,并且已经有 8 张图片代表动画的每一帧。这是我的代码(使用 libgdx Animation 类):

public class Coin extends GameObject implements Screen {
private SpriteBatch batch;
private Animation animation;
private float time;

public Coin(Sprite spr, float xPos, float yPos,
        float radius) {
    super(spr, xPos, yPos, radius);
    setxPos(xPos);
    setyPos(yPos);
    batch = new SpriteBatch();
    time = 0;
}

public void render(float delta) {
// TODO Auto-generated method stub
batch.begin();
batch.draw(animation.getKeyFrame(time += delta), getxPos(), getyPos());
batch.end();
}


@Override
public void resize(int width, int height) {
    // TODO Auto-generated method stub

}

@Override
public void show() {
    // TODO Auto-generated method stub
    //batch = new SpriteBatch();
    animation = new Animation(1 / 3f, 
            new TextureRegion(new Texture("coin1.png")), 
            new TextureRegion(new Texture("coin2.png")),
            new TextureRegion(new Texture("coin3.png")), 
            new TextureRegion(new Texture("coin4.png")), 
            new TextureRegion(new Texture("coin5.png")), 
            new TextureRegion(new Texture("coin6.png")), 
            new TextureRegion(new Texture("coin7.png")), 
            new TextureRegion(new Texture("coin8.png")));
    animation.setPlayMode(Animation.PlayMode.LOOP);
}
@Override
public void hide() {
    // TODO Auto-generated method stub

}

@Override
public void pause() {
    // TODO Auto-generated method stub

}

@Override
public void resume() {
    // TODO Auto-generated method stub

}

@Override
public void dispose() {
    // TODO Auto-generated method stub
    batch.dispose();
}

}

我从 LogCAT 得到的错误是 NullPointerException @batch.draw(animation.getKeyFrame(time += delta), getxPos(), getyPos());

有谁知道如何解决这个问题?任何见解将不胜感激

【问题讨论】:

  • 我的猜测是 batchanimation 出于某种原因为空。可能不是batch,因为您在该行之前调用了begin()。你能找出animation 可能为空的任何原因吗? render() 是否在 show() 之前被调用?
  • 确保将 coin.png 存储在 assets 文件夹中并使用 Texture(Gdx.files.internal("coin.png")) 调用它。
  • 已经尝试重新排列 render() 和 show() 但这也不起作用。你说得对,我应该这样加载我的纹理,谢谢

标签: java android animation libgdx 2d


【解决方案1】:

写下这段代码:

public Coin(Sprite spr, float xPos, float yPos,
    float radius) {
super(spr, xPos, yPos, radius);
setxPos(xPos);
setyPos(yPos);
batch = new SpriteBatch();
time = 0;
loadAnimation();
}




 public void loadAnimation() {
    // TODO Auto-generated method stub
    //batch = new SpriteBatch();
    animation = new Animation(1 / 3f, 
            new TextureRegion(new Texture("coin1.png")), 
            new TextureRegion(new Texture("coin2.png")),
            new TextureRegion(new Texture("coin3.png")), 
            new TextureRegion(new Texture("coin4.png")), 
            new TextureRegion(new Texture("coin5.png")), 
            new TextureRegion(new Texture("coin6.png")), 
            new TextureRegion(new Texture("coin7.png")), 
            new TextureRegion(new Texture("coin8.png")));
    animation.setPlayMode(Animation.PlayMode.LOOP);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    相关资源
    最近更新 更多