【问题标题】:NullPointerException in Basic libGDX Tower Defense Game基本 libGDX 塔防游戏中的 NullPointerException
【发布时间】:2020-11-13 21:39:12
【问题描述】:

我正在尝试使用 libGDX 开发一款小型塔防游戏,但遇到以下异常: 线程“LWJGL 应用程序”中的异常 java.lang.NullPointerException 在 com.badlogic.gdx.graphics.g2d.SpriteBatch.switchTexture(SpriteBatch.java:1067) 在 com.badlogic.gdx.graphics.g2d.SpriteBatch.draw(SpriteBatch.java:558) 在 com.badlogic.gdx.graphics.g2d.Sprite.draw(Sprite.java:517) 在levels.AISprite.draw(AISprite.java:33) 在levels.levelGenerator.render(levelGenerator.java:44) 在 com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.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() ;
    }
}

【问题讨论】:

    标签: java nullpointerexception libgdx game-physics


    【解决方案1】:

    使用调试器,在levels.levelGenerator.render(levelGenerator.java:44) 上设置断点,并检查导致错误的原因....如果您从未使用过调试器,我强烈(!!!)推荐您在继续使用 libgdx 之前了解如何使用它

    【讨论】:

      【解决方案2】:

      确保在调用 create() 方法之后进行所有 Gdx 调用。在此之前所有 gdx 对象都为 null,因此诸如 Gdx.graphics.(...)Gdx.app.(...)Gdx.audio.(...) 等的调用将引发 NPE

      【讨论】:

        猜你喜欢
        • 2013-07-10
        • 1970-01-01
        • 2013-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多