【问题标题】:NullPointerException is Java while trying to Animate with libgdxNullPointerException 是 Java,同时尝试使用 libgdx 进行动画处理
【发布时间】:2017-04-25 20:38:39
【问题描述】:

当我尝试创建我的播放器并为其设置动画时,我得到了一个 nullPointerException。我真的不明白怎么了。 代码如下:

package com.sheep.game.Sprites;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.sheep.game.Screens.PlayScreen;

public class Player extends Sprite {

    float elapsedTime = 0;

    public Animation<TextureRegion> animation;
    public Texture sheepLoop2;
    TextureRegion[] animationFrames;

    public Sprite spr_player;

    private PlayScreen screen;

    public Player(PlayScreen screen) {

        this.screen = screen;

        sheepLoop2 = new Texture("sheepLoop2.png");

        TextureRegion[][] frames = TextureRegion.split(sheepLoop2, 33, 21);

        animationFrames = new TextureRegion[2];
        int index = 0;

        for(int i = 0; i < 1; i++) {
            for(int j = 0; j < 1; j++) {
                animationFrames[index++] = frames[j][i];
            }
        }

        animation = new Animation<TextureRegion>(1f/30f, animationFrames);

        spr_player = new Sprite(sheepLoop2);



     }

    public void update() {

        spr_player.setX(10);
        spr_player.setY(100);
        spr_player.setRegion(animation.getKeyFrame(elapsedTime,true));

    }

    public void render() {

        elapsedTime += Gdx.graphics.getDeltaTime();

    }

    public Texture getSheepLoop2() {
        return sheepLoop2;
    }
}

StackTrace 是:

Exception in thread "LWJGL Application" java.lang.NullPointerException
    at com.badlogic.gdx.graphics.g2d.TextureRegion.setRegion(TextureRegion.java:112)
    at com.sheep.game.Sprites.Player.update(Player.java:53)
    at com.sheep.game.Screens.PlayScreen.update(PlayScreen.java:61)
    at com.sheep.game.Screens.PlayScreen.render(PlayScreen.java:84)
    at com.badlogic.gdx.Game.render(Game.java:46)
    at com.sheep.game.SheepGame.render(SheepGame.java:28)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:225)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)

【问题讨论】:

  • Exception in thread "LWJGL Application" java.lang.NullPointerException at com.sheep.game.Sprites.Player.update(Player.java:53) 53 是哪一行?那里有什么可能是空的?当您在该区域周围进行调试时,您会发现什么?
  • 检查animationFrames是否正确初始化。

标签: java libgdx


【解决方案1】:
animationFrames = new TextureRegion[2]; // having size 2 array initialisation not TextureRegion

for(int i = 0; i < 1; i++) {
     for(int j = 0; j < 1; j++) {
         animationFrames[index++] = frames[j][i]; //run once according to your condition
    }
}

通过上面的循环你只初始化第一个索引,循环之后animationFrames[1]为空

这里有例外

animation.getKeyFrame(elapsedTime,true) //return second TextureRegion that is still null

将空 TextureRegion 设置为 Sprite

spr_player.setRegion(animation.getKeyFrame(elapsedTime,true));

编辑

如果两个精灵帧都是水平排列的

for(int i = 0; i < 1; i++) {
    for(int j = 0; j < 2; j++) {  
       animationFrames[index++] = frames[i][j];
   }
}

【讨论】:

  • 我只是在学习,我并没有真正明白你的意思?我该如何解决这个问题?
  • 有两帧都是 33 x 21 像素
猜你喜欢
  • 1970-01-01
  • 2017-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-12
  • 2020-07-10
  • 1970-01-01
相关资源
最近更新 更多