Java LibGDX-试图制作一个精灵的动画,破坏了我的整个游戏
我正在开发一款Android游戏,到目前为止,我绘制游戏对象的方式是在游戏中初始化它们,然后将它们放入游戏对象的数组列表中。每个对象都扩展了这个抽象类;玩家、旗帜、硬币。该数组列表被传递到渲染器中,渲染器然后使用在数组列表上迭代的for循环来绘制对象 我正在尝试添加一个名为硬币的新游戏对象。与其他对象不同,我希望这一个被设置动画,并且已经有8张图片代表动画的每一帧。下面是我使用libgdx动画类的代码:Java LibGDX-试图制作一个精灵的动画,破坏了我的整个游戏,java,android,animation,libgdx,2d,Java,Android,Animation,Libgdx,2d,我正在开发一款Android游戏,到目前为止,我绘制游戏对象的方式是在游戏中初始化它们,然后将它们放入游戏对象的数组列表中。每个对象都扩展了这个抽象类;玩家、旗帜、硬币。该数组列表被传递到渲染器中,渲染器然后使用在数组列表上迭代的for循环来绘制对象 我正在尝试添加一个名为硬币的新游戏对象。与其他对象不同,我希望这一个被设置动画,并且已经有8张图片代表动画的每一帧。下面是我使用libgdx动画类的代码: public class Coin extends GameObject implement
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.drawanimation.getKeyFrametime+=delta、getxPos、getyPos
有人知道如何解决这个问题吗?如果您有任何见解,我们将不胜感激请编写以下代码:
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);
}
我的猜测是批处理或者动画由于某种原因是空的。可能不是批处理,因为您在该行之前调用了begin。你能找出动画可能为空的任何原因吗?是否在显示前调用渲染?请确保将coin.png存储在资产文件夹中,并使用TextureGdx.files.internalcoin.png调用它。您已经尝试重新排列渲染和显示,但也没有成功。你是对的,我应该用这种方式加载我的纹理,谢谢