PlayScreen类:
public class PlayScreen implements Screen {
private Player player;
private OrthographicCamera camera;
private OrthogonalTiledMapRenderer renderer;
private TiledMap map;
private Rectangle rightRectangle, leftRectangle, playerRectangle;
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.render();
renderer.getSpriteBatch().begin();
player.draw(renderer.getSpriteBatch());
renderer.getSpriteBatch().end();
}
@Override
public void resize(int width, int height) {
camera.viewportWidth = width;
camera.viewportHeight = height;
camera.update();
}
@Override
public void show() {
camera = new OrthographicCamera();
map = new TiledMap();
renderer = new OrthogonalTiledMapRenderer(map);
player = new Player(new Sprite(new Texture("img/player.png")), playerRectangle);
playerRectangle = player.rectangle;
rightRectangle = new Rectangle(1280,0,0,720);
leftRectangle = new Rectangle(0,0,0,720);
boolean wallLeft = leftRectangle.overlaps(player.rectangle);
boolean wallRight = rightRectangle.overlaps(player.rectangle);
if(wallLeft){
System.out.println("wallLeft Overlap");
player.velocity.x = 0;
}
else if(wallRight){
System.out.println("wallRight Overlap");
player.velocity.x = 0;
}
player.setPosition(Gdx.graphics.getWidth()/2f - player.getWidth()/2f,Gdx.graphics.getHeight()/2f - player.getHeight()/2f - Gdx.graphics.getHeight()/5f);
}
@Override
public void hide() {
dispose();
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
map.dispose();
player.getTexture().dispose();
renderer.dispose();
}
}
玩家等级:
public class Player extends Sprite{
public Vector2 velocity = new Vector2();
private float speed = 500;
public Rectangle rectangle;
public Player(Sprite sprite, Rectangle rectangle){
super(sprite);
this.rectangle = rectangle;
}
public void draw(SpriteBatch spriteBatch){
update(Gdx.graphics.getDeltaTime());
super.draw(spriteBatch);
}
public void update(float delta) {
rectangle = new Rectangle(getX() + velocity.x * delta,0,286,160);
setX(getX() + velocity.x * delta);
}
}
所以,我正在使用矩形来检测我的碰撞。我已将rightRectangle和leftRectangle设置为我的屏幕边界。我想为玩家分配一个精灵和一个矩形。我正在处理PlayScreen类中的碰撞,但它导致了NullPointerException。需要帮助找出为什么我得到NullPointerException。
谢谢!
的NullPointerException:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.badlogic.gdx.math.Rectangle.overlaps(Rectangle.java:210)
at net.dermetfan.asteroids.screens.PlayScreen.show(PlayScreen.java:62)
at com.badlogic.gdx.Game.setScreen(Game.java:62)
at net.dermetfan.asteroids.screens.MainMenu$1.clicked(MainMenu.java:75)
at
com.badlogic.gdx.scenes.scene2d.utils.ClickListener.touchUp(ClickListener.java:82)
at com.badlogic.gdx.scenes.scene2d.InputListener.handle(InputListener.java:57)
at com.badlogic.gdx.scenes.scene2d.Stage.touchUp(Stage.java:329)
at com.badlogic.gdx.backends.lwjgl.LwjglInput.processEvents(LwjglInput.java:297)
at
com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:209)
at
com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
答案 0 :(得分:1)
问题在于你的playerRectangle。
player = new Player(new Sprite(new Texture("img/player.png")), playerRectangle); // playerRectangle not initialized!
playerRectangle = player.rectangle;
rightRectangle = new Rectangle(1280,0,0,720);
leftRectangle = new Rectangle(0,0,0,720);
尝试这样的事情
playerRectangle=new Rectangle(x,y,w,h);
player = new Player(new Sprite(new Texture("img/player.png")), playerRectangle);
playerRectangle = player.rectangle; // i don't even think you should put this line is useless
rightRectangle = new Rectangle(1280,0,0,720);
leftRectangle = new Rectangle(0,0,0,720);
现在你不应该有任何错误。
或者更好地在你的玩家类中执行此操作,在主类中不需要playerRectangle
public Player(Sprite sprite){
super(sprite);
this.rectangle = sprite.getBoundingRectangle();
}
在你的课堂上
player = new Player(new Sprite(new Texture("img/player.png")));
rightRectangle = new Rectangle(1280,0,0,720);
leftRectangle = new Rectangle(0,0,0,720);