2014-10-19 47 views
0

我做一個簡單的Java遊戲,因爲我的一部分,它性學習的,當我運行它,我得到一個:的NullPointerException在libgdx(Java)的重疊

Exception in thread "LWJGL Application" java.lang.NullPointerException at com.mattihase.floatingpoint.GameScreen.generalUpdate(GameScreen.java:65) at com.mattihase.floatingpoint.GameScreen.render(GameScreen.java:42) at com.badlogic.gdx.Game.render(Game.java:46) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:207) at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)

這是一般更新線就在談論和它下面的直線:

if(pc.bounds.overlaps(gary.Rbounds)) 
    //where i test a bunch of booleans to see what the dialog returned should be 
    //yes, i know i'm probably doing the ifs it unoptimized but i couldn't get it to 
    //work the other way. 
    //the pc.bounds stuff is there to make it follow the player character around the 
    //world as the camera already does this. 
     { 
     if(pc.onquest = false){ 
      if(pc.finishquest = false){ 
      Assets.font.draw(batch, gary.Gdio, pc.bounds.x - 250, pc.bounds.y - 190); 
      pc.onquest = true; 
      } 
     } 
     if(pc.onquest = true){ 
      if(pc.finishquest = false){ 
      Assets.font.draw(batch, gary.GDdio, pc.bounds.x - 250, pc.bounds.y - 190); 
      } 
      } 
     if(pc.haspoints = true){ 
      pc.finishquest = true; 
     } 
     if(pc.finishquest = true); 
     Assets.font.draw(batch, gary.GCdio, pc.bounds.x - 250, pc.bounds.y - 190); 
     pc.onquest = false; 
     pc.award = true; 
    }` 

和這些都爲playercharacter和NPC(稱爲PC(類playerCharacter)和加里類(CLA SS Rectoid)分別) 玩家角色

public Sprite image; 
public Rectangle bounds; 
public int score; 
public String scoreprint; 
public boolean onquest; 
public boolean finishquest; 
public boolean haspoints; 
public boolean award; 

public PlayerCharacter(){ 
    onquest = false; 
    haspoints = false; 
    finishquest = false; 
    award = false; 
    score = 0; 
    image = Assets.s_pc; 
    bounds = new Rectangle(256-16, 192-16, 16, 16); 
    scoreprint = score + "";` 

和加里 '公共雪碧銳美; public Rectangle Rbounds; public String Gdio; public String GDdio; public String GCdio;

public Rectoid() 
{ 
    Rimage = Assets.s_rectoid; 
    Rbounds = new Rectangle(1024, 64, 64, 32); 
    Gdio = "the diolog"; 
    GDdio = "the diolog"; 
    GCdio = "the diolog";` 

Ç

+0

您是否知道像'pc.onquest = false'這樣的句子是否會返回賦值? – Ezequiel 2014-10-19 21:55:42

+0

我有一點n00b,請你詳細說明爲什麼這可能是一個問題。 – mattihase 2014-10-20 07:11:49

回答

1

在Java中,檢查平等對象的引用或基本類型,你必須使用==運營商。在if(pc.onquest = false){這樣的行中,您將false指定爲onquest,而不是檢查其值。 代碼編譯的原因是因爲在Java中,賦值返回賦值。 例如上一句:

int zero = 0; 
if (zero == 0){} //its true 
if (zero = 0){} //compile error, becouse 'zero = 0' returns '0', the asigned value 

什麼在你的代碼的情況是,您使用的布爾變量。

if(pc.bounds.overlaps(gary.Rbounds)) 
    { 
    if(pc.onquest = false){ // HERE YOU ARE DOING TWO THINGS: assing false to 
          // onquest and return false. The following code is never execute 
     if(pc.finishquest = false){ // SAME HERE 
     Assets.font.draw(batch, gary.Gdio, pc.bounds.x - 250, pc.bounds.y - 190); 
     pc.onquest = true; 
     } 
    } 
    if(pc.onquest = true){// SAME HERE 
     if(pc.finishquest = false){// SAME HERE 
     Assets.font.draw(batch, gary.GDdio, pc.bounds.x - 250, pc.bounds.y - 190); 
     } 
     } 
    if(pc.haspoints = true){// SAME HERE 
     pc.finishquest = true; 
    } 
    if(pc.finishquest = true);// SAME HERE 
    Assets.font.draw(batch, gary.GCdio, pc.bounds.x - 250, pc.bounds.y - 190); 
    pc.onquest = false; 
    pc.award = true; 
}` 

此外,檢查一個布爾值只是用它,沒有把它比作真或假,並利用!運營商。

只是簡單的使用if(!pc.onquest){如果你想檢查是否禁用了onquest。

關於NullPointerEception,可能gary變量爲null。檢查你之前是否創建過它。它應該是這樣的:

Rectoid gary = new Rectoid();