2014-06-20 59 views
0

播放屏幕類:LIBGDX:調用另一個類中的矩形導致NullPointerException?

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

} 

} 

Player類:

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); 
+0

對不起,我應該加上這一行: playerRectangle = new Rectangle(x,y,w,h); 並使x,y,w和h浮動?我試圖不添加這一行,並刪除playerRectangle = player.rectangle;並且仍然遇到錯誤。 – user3567126

+0

你需要初始化它。檢查我的編輯,因爲你真的不需要你的主類中的播放器矩形 –

+0

謝謝!我去了你的編輯,並刪除了我的主類中的playerRectangle。原本我從來沒有這樣做,並一直襬弄它,並決定離開playerRectangle在我的主類。但是,無論如何,它拿出來後,運行良好(?),但沒有碰撞發生。出於某種原因,leftRectangle和rightRectangle從不與玩家重疊。你知道它爲什麼不重疊嗎? – user3567126

相關問題