gpt4 book ai didi

java - Libgdx 错误(NullPoinerException)

转载 作者:行者123 更新时间:2023-12-01 10:26:07 27 4
gpt4 key购买 nike

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

我是 Libgdx 的新手,在尝试创建游戏时发现了错误。我正在使用《学习 Libgdx 游戏开发》这本书,但我的项目中出现错误。

错误是:

Exception in thread "LWJGL Application" java.lang.NullPointerException at com.keerp.game.WorldRenderer.renderTestObjects(WorldRenderer.java:43) at com.keerp.game.WorldRenderer.render(WorldRenderer.java:35) at com.keerp.game.MainClass.render(MainClass.java:62) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:223) at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)

这是我的代码:

主类:

    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;

}

世界 Controller :

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)
{

}

}

世界渲染器:

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();
}
}

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

谢谢!

最佳答案

WorldController类的initTestObjects()函数定义并初始化了成员变量Sprite[] testSprites。但它没有在任何地方被调用。您在 WorldRenderer.java 的第 -44 行中收到 NullPointerException,因为 testSprites vairable 为 null,并且您正在尝试迭代它以获取其内容。

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

 private void init()
{

initTestObjects(); }

关于java - Libgdx 错误(NullPoinerException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35354977/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com