gpt4 book ai didi

java - LibGDX 异常 : Exception in thread "LWJGL Application" java. lang.NullPointerException

转载 作者:行者123 更新时间:2023-12-01 22:06:48 24 4
gpt4 key购买 nike

我的代码过去工作得很好,但现在当我启动应用程序时,我得到一个空白屏幕和一个错误。

“线程“LWJGL应用程序”中出现异常java.lang.NullPointerException 在 com.mygdx.game.Bunny.render(Bunny.java:70) 在 com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215) 在 com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)"

这表示第 70 行:

batch.draw(paddle, paddleRect.x, paddleRect.y);

我的完整代码是:

package com.mygdx.game;

import java.util.Iterator;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.Array;

public class Bunny extends ApplicationAdapter {
SpriteBatch batch;
Texture paddle;
Texture blockImg;
Rectangle paddleRect;

float speed = 300.0f;

private OrthographicCamera camera;

private Array<Rectangle> blocks;

private void spawnBlock(int x, int y){
Rectangle block = new Rectangle();
block.x = x*90;
block.y = 400-(y*30);
block.width = 30;
block.height = 10;
blocks.add(block);
}

@Override
public void create () {
Rectangle paddleRect = new Rectangle();

paddleRect.x = 0;
paddleRect.y = 0;
paddleRect.width = 60;
paddleRect.height = 20;

batch = new SpriteBatch();
paddle = new Texture("paddle.png");
blockImg = new Texture("block.png");

camera = new OrthographicCamera();
camera.setToOrtho(false, 400, 400);

blocks = new Array<Rectangle>();

for(int x = 0; x < 5; x++){
for(int y = 0; y < 4; y++){
spawnBlock(x, y);
}
}

}

@Override
public void render () {
Gdx.gl.glClearColor(0.5f, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();

batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(paddle, paddleRect.x, paddleRect.y);
for(Rectangle block: blocks){
batch.draw(blockImg, block.x, block.y);
}
batch.end();

if(Gdx.input.isKeyPressed(Input.Keys.RIGHT))
paddleRect.x += speed * Gdx.graphics.getDeltaTime();
else if(Gdx.input.isKeyPressed(Input.Keys.LEFT))
paddleRect.x -= speed * Gdx.graphics.getDeltaTime();
if(Gdx.input.isKeyPressed(Input.Keys.UP))
paddleRect.y += speed * Gdx.graphics.getDeltaTime();

if(paddleRect.x < 0f)
paddleRect.x = 0f;
if(paddleRect.x > 340f)
paddleRect.x = 340f;

Iterator<Rectangle> iter = blocks.iterator();
while(iter.hasNext()){
Rectangle block = iter.next();
if(block.overlaps(paddleRect))
iter.remove();
}
}
}

最佳答案

create 中,您想要为类字段 paddleRect 分配一个新的 Rectangle,但您创建了一个名为 paddleRect 的方法变量 并且您的类字段仍为 null

尝试:

public void create () {
paddleRect = new Rectangle();

paddleRect.x = 0;
paddleRect.y = 0;
...

关于java - LibGDX 异常 : Exception in thread "LWJGL Application" java. lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32581177/

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