nullpointerexception

ff29svar  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(264)

我正在尝试用libgdx开发一个小型的塔防游戏,得到以下异常:com.badlogic.gdx.graphics.g2d.spritebatch.switchtexture(spritebatch)上的线程“lwjgl application”java.lang.nullpointerexception中的异常。java:1067)在com.badlogic.gdx.graphics.g2d.spritebatch.draw(spritebatch。java:558)在com.badlogic.gdx.graphics.g2d.sprite.draw(精灵。java:517)在levels.aisprite.draw(aisprite。java:33)在levels.levelgenerator.render(levelgenerator。java:44)在com.badlogic.gdx.backends.lwjgl.lwjglaplication.mainloop(lwjglaplication。java:232)在com.badlogic.gdx.backends.lwjgl.lwjglapplication$1.run(lwjglapplication。java:127)
我知道有些东西没有初始化,但是什么?如果你能帮我解释一下问题所在,我会非常高兴的。如果你能改进我的代码,我也会很高兴的!提前谢谢
代码(仅在魔法发生的地方):levelgenerator类

public class levelGenerator extends ApplicationAdapter {
    private final String level;
    SpriteBatch batch;
    Texture img;
    Scorpion scorpion;
    private Array<AISprite> aiSprites;

    public levelGenerator(String level){
        this.level = level;
    }

    @Override
    public void create () {
        Gdx.graphics.setTitle("Tower Defense Game");
        scorpion = new Scorpion();
        img = new Texture(level);
        scorpion.createImage();
        aiSprites = new Array<AISprite>();
        aiSprites.add(new AISprite(scorpion, LevelOne.levelOnePath()));

    }

    public void render () {
        batch = new SpriteBatch();
        batch.begin();
        //just the level image background
        batch.draw(img, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        scorpion.renderImage();

        for(AISprite aiSprite: aiSprites){

            aiSprite.draw(batch);

        }

        for(AISprite aiSprite: aiSprites){

            Vector2 previous = aiSprite.getPath().first();

            for(Vector2 waypoint: aiSprite.getPath()){
                previous = waypoint;
            }
        }

    }

    @Override
    public void dispose () {
        batch.dispose();
        img.dispose();
        scorpion.disposeImage();

    }
}

实体类:

public class Entity extends Sprite {

    String atlasPath;
    private Animation<TextureRegion> animation;
    private TextureAtlas entityAtlas;
    private float timePassed = 0;
    SpriteBatch batch;

    public Entity(String atlasPath){
        this.atlasPath = atlasPath;
    }

    public void createImage(){

        // path for scorpion atlas file: "assetsPack/scorpions/scorpionRunning/scorpionPack.atlas"
        entityAtlas = new TextureAtlas(Gdx.files.internal(atlasPath));
        animation = new Animation<TextureRegion>(1/40f, entityAtlas.getRegions());
    }

    public void renderImage(){
        batch = new SpriteBatch();
        batch.begin();
        timePassed += Gdx.graphics.getDeltaTime();
        batch.draw(animation.getKeyFrame(timePassed, true), 0, 0);
        batch.end();
    }

    public void disposeImage(){
        entityAtlas.dispose();
    }
}

蝎子(实体)类:

public class Scorpion extends Entity {

    public Scorpion(){
        super("assetsPack/scorpions/scorpionRunning/scorpionPack.atlas");

    }

下一个类用于实体的寻路

public class AISprite extends Sprite {

    private Vector2 velocity = new Vector2();
    private float speed = 100, tolerance = 3;

    public Array<Vector2> getPath() {
        return path;
    }

    private Array<Vector2> path;

    private int waypoint = 0;

    public AISprite(Entity entity, Array<Vector2> path){
        super(entity);
        this.path = path;
    }

    public void draw(SpriteBatch spriteBatch){
        update(Gdx.graphics.getDeltaTime());
        super.draw(spriteBatch);
    }
    public void update(float delta){
        float angle = (float) Math.atan2(path.get(waypoint).y - getY(), path.get(waypoint).x - getX());
        velocity.set((float) Math.cos(angle) * speed, (float) Math.sin(angle) * speed);

        setPosition(getX() + velocity.x * delta, getY() + velocity.y * delta);

        if(isWaypointReached()){
            setPosition(path.get(waypoint).x, path.get(waypoint).y);
            if(waypoint + 1 >= path.size){
                waypoint = 0;
            }
            else{
                waypoint++;
            }
        }

    }
    public boolean isWaypointReached(){
        return path.get(waypoint).x - getX() <= speed / tolerance * Gdx.graphics.getDeltaTime() && path.get(waypoint).y - getY() <= speed / tolerance * Gdx.graphics.getDeltaTime() ;
    }
}
gijlo24d

gijlo24d1#

确保所有的gdx调用都是在 create() 方法被调用。所有gdx对象在该点之前都为null,因此 Gdx.graphics.(...) 或者 Gdx.app.(...) 或者 Gdx.audio.(...) etc将抛出一个npe

gfttwv5a

gfttwv5a2#

使用调试器,在levels.levelgenerator.render(levelgenerator)上设置断点。java:44),并检查导致错误的原因。。。。如果你从未使用过调试器,我强烈(!!!)建议您在继续使用libgdx之前先学习如何使用它

相关问题