Libgdx错误(NullPoinerException)

时间:2016-02-12 04:43:41

标签: java libgdx

(如果我的英语错了,我很抱歉,我是巴西人。)

我是Libgdx的新手,我在尝试创建游戏时发现了一个错误。我正在使用“学习Libgdx游戏开发”一书,我的项目出错了。

错误是:

  

线程“LWJGL Application”中的异常java.lang.NullPointerException   在com.keerp.game.WorldRenderer.renderTestObjects(WorldRenderer.java:43)         在com.keerp.game.WorldRenderer.render(WorldRenderer.java:35)         在com.keerp.game.MainClass.render(MainClass.java:62)         在com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:223)         在com.badlogic.gdx.backends.lwjgl.LwjglApplication $ 1.run(LwjglApplication.java:124)

这是我的代码:

MainClass:

    package com.keerp.game;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.ApplicationListener;

public class MainClass implements ApplicationListener {

    private static final String TAG = MainClass.class.getName();

    private WorldController worldController;
    private WorldRenderer worldRenderer;

    private boolean paused;


    @Override
    public void create () 
    {
        // set libgdx log level to DEBUG    
        Gdx.app.setLogLevel(Application.LOG_DEBUG);

        // initialize controller and renderer
        worldController = new WorldController();
        worldRenderer = new WorldRenderer(worldController);

        // game world is active on start
        paused = false;

    }

    @Override
    public void dispose()
    {
        worldRenderer.dispose();
    }


    @Override
    public void render () {

        if(!paused)
        {
            worldController.update(Gdx.graphics.getDeltaTime());
        }

        // setting screen color
        Gdx.gl.glClearColor(0x64/255.0f, 0x95/255.0f, 0xed/255.0f, 0xff/255.0f);

        // clear the screen
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        // render game world to screen
        worldRenderer.render();
    }

    @Override
    public void resize(int width, int height)
    {
        worldRenderer.resize(width, height);
    }

    @Override
    public void pause()
    {
        paused = true;
    }

    @Override
    public void resume()
    {
        paused = false;
    }
}

常量:

package com.keerp.game;

public class Constants {

    // setting constants tag
    private static final String TAG = Constants.class.getName();

    public static final float VIEWPORT_WIDTH = 5.0f;

    public static final float VIEWPORT_HEIGHT = 5.0f;

}

WorldController:

package com.keerp.game;

import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.MathUtils;

public class WorldController {

    private static final String TAG = WorldController.class.getName();

    public Sprite[] testSprites;
    public int selectedSprite;


    public WorldController()
    {
        init();
    }

    private void initTestObjects()
    {
        testSprites = new Sprite[5];

        int width = 32;
        int height = 32;

        Pixmap pixmap = createProceduralPixmap(width, height);

        Texture texture = new Texture(pixmap);

        for(int i = 0; i < testSprites.length; i++)
        {
            Sprite spr = new Sprite(texture);
            spr.setSize(1, 1);
            spr.setOrigin(spr.getWidth() / 2.0f, spr.getHeight() / 2.0f);

            // calculating random position for each sprite

            float randomX = MathUtils.random(-2.0f, 2.0f);
            float randomY = MathUtils.random(-2.0f, 2.0f);

            spr.setPosition(randomX, randomY);

            // put new sprite into array
            testSprites[i] = spr;

        }

        selectedSprite = 0;
    }


    private Pixmap createProceduralPixmap(int width, int height)
    {
        Pixmap pixmap = new Pixmap(width, height, Format.RGBA8888);

        // fill square with red color at 50% opacity
        pixmap.setColor(1, 0, 0, 0.5f);
        pixmap.fill();

        // draw a yellow-colored X shape on square
        pixmap.setColor(1, 1, 0, 1);
        pixmap.drawLine(0, 0, width, height);
        pixmap.drawLine(width, 0, 0, height);

        // draw a cyan-colored border around square

        pixmap.setColor(0, 1, 1, 1);
        pixmap.drawRectangle(0, 0, width, height);


        return pixmap;
    }

    private void updateTestObjects(float deltaTime)
    {
        // get current rotation from selected sprite
        float rotation = testSprites[selectedSprite].getRotation();

        // rotate sprite by 90 degrees per second 
        rotation += 90 * deltaTime;

        // wrap around at 360 degrees
        rotation %= 360;

        // set new rotation value to selected sprite
        testSprites[selectedSprite].setRotation(rotation);


    }

    private void init()
    {

    }

    public void update(float deltaTime)
    {

    }

}

WorldRenderer:

package com.keerp.game;

import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.Disposable;

public class WorldRenderer implements Disposable 
{

    private static final String TAG = WorldRenderer.class.getName();

    private OrthographicCamera camera;
    private SpriteBatch batch;
    private WorldController worldController;


    public WorldRenderer(WorldController worldController)
    {
        this.worldController = worldController;
        init();
    }

    private void init()
    {
        batch = new SpriteBatch();
        camera = new OrthographicCamera(Constants.VIEWPORT_WIDTH, Constants.VIEWPORT_HEIGHT);
        camera.position.set(0, 0, 0);
        camera.update();
    }


    public void render()
    {
        renderTestObjects();
    }

    private void renderTestObjects()
    {
        batch.setProjectionMatrix(camera.combined);
        batch.begin();

        for(Sprite sprite : worldController.testSprites)
            sprite.draw(batch);

        batch.end();
    }

    public void resize(int width, int height)
    {
        camera.viewportWidth = (Constants.VIEWPORT_HEIGHT / height) * width;
        camera.update();
    }

    @Override
    public void dispose()
    {
        batch.dispose();
    }
}

我不知道解决这个问题是否容易,但我真的需要帮助。

谢谢!

1 个答案:

答案 0 :(得分:1)

WorldController类的initTestObjects()定义并初始化成员变量-Sprite [] testSprites。但它并没有被称为任何地方。 你在WorldRenderer.java的-44行中得到NullPointerException,因为testSprites vairable为null,你试图迭代它的内容。

请在WorldController.class中包含initTestObjects()代码。

 private void init()
{

initTestObjects();     }