2014-01-05 58 views
2

我使用slick2D創​​建一個簡單的java遊戲。不幸的是我的Java技能比簡單更簡單。這個遊戲將是一個記憶遊戲,因爲我認爲它會很簡單。我遇到的問題之一是我有一個NormalMode類和一個Square類。我在普通模式類中創建了一個Square對象來調用Square的方法。所以,我試圖調用一個有「圖形g」作爲邊界的方法,結果我得到一個空指針異常錯誤(或者其他我不知道的東西)。調用圖形g作爲外圍的方法

廣場類:(這個類中的最後一個方法是未完成的那一刻)

 package data.src; 

import java.util.Random; 

import org.newdawn.slick.Graphics; 
import org.newdawn.slick.Image; 
import org.newdawn.slick.SlickException; 
import org.newdawn.slick.state.StateBasedGame; 

public class Square { 

    private StateBasedGame game; 
    boolean correct; 
    boolean clickable; 
    boolean clicked; 
    boolean started; 
    int squares; 
    int squareX; 
    int squareY; 
    public Image squareIncorrect; 
    public Image squareCorrect; 

    public Square() { 

       } 



    public boolean checkCorrect(){ 
     return correct; 

    } 
    public boolean checkClickable(){ 
     return clickable; 
    } 

    public boolean checkClicked(){ 
     return clicked; 

    } 


     public void squareStart(int x ,int y, Graphics g){ 

      if(correct == true){ 
       g.drawImage(squareCorrect, x, y); 
      } 
      else if(correct == false){ 
       g.drawImage(squareIncorrect, x, y); 
      } 
      g.drawString("Remember the blue squares!", x, y); 
      try { 
       wait(3000); 
      } catch (InterruptedException e) { 

       e.printStackTrace(); 
      } 
      started = true; 
      while(started == true){ 
       try { 
        squareCorrect = new Image("res/squareCorrect"); 
       } catch (SlickException e) { 

        e.printStackTrace(); 
       } 

      } 

     } 

    public void createRandom(){ 

     Random rand = new Random(); 
     int sRand = rand.nextInt(1); 

      if(sRand == 0){ 
       correct = false; 
       clickable = true; 
       clicked = false; 

      } 
      else if(sRand == 1){ 
       correct = true; 
       clickable = true; 
       clicked = false; 
       squares++; 

      } 
    } 


    public void youWin(int x, int y, Graphics g){ 
     if(squares == 0){ 
      g.drawString("You win!", x, y); 
      game.enterState(0); 
     } 
    } 
} 

普通模式類型:

package data.src; 

import org.newdawn.slick.GameContainer; 
import org.newdawn.slick.Graphics; 
import org.newdawn.slick.SlickException; 
import org.newdawn.slick.state.BasicGameState; 
import org.newdawn.slick.state.StateBasedGame; 

public class NormalMode extends BasicGameState { 

    private Square[][] square = new Square[4][4]; 

    public NormalMode() { 


       } 

    @Override 
    public void init(GameContainer arg0, StateBasedGame Game)throws SlickException { 
        for(int i = 0; i < square.length; i++){ 
         for(int j = 0; j < square.length; j++){ 
          square[i][j] = new Square(); 
          square[i][j].createRandom(); 
         } 
        } 

      } 

    @Override 
    public void render(GameContainer container, StateBasedGame game, Graphics g) 
      throws SlickException{ 
       square[1][1].squareStart(40, 300, g); 

    } 

    @Override 
    public void update(GameContainer container, StateBasedGame game, int delta) 
      throws SlickException { 


    } 

    @Override 
    public int getID() { 
     return 3; 
    } 


} 

PS:請原諒我的東西,完全是胡說八道,如果我的代碼似乎笨。

錯誤:

Sun Jan 05 01:17:25 MST 2014 ERROR:null 
java.lang.NullPointerException 
    at org.newdawn.slick.Graphics.drawImage(Graphics.java:1384) 
    at org.newdawn.slick.Graphics.drawImage(Graphics.java:1433) 
    at data.src.Square.squareStart(Square.java:46) 
    at data.src.NormalMode.render(NormalMode.java:32) 
    at org.newdawn.slick.state.StateBasedGame.render(StateBasedGame.java:199) 
    at org.newdawn.slick.ScalableGame.render(ScalableGame.java:158) 
    at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:688) 
    at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:411) 
    at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:321) 
    at data.src.Core.main(Core.java:30) 
Sun Jan 05 01:17:25 MST 2014 ERROR:Game.render() failure - check the game code. 
org.newdawn.slick.SlickException: Game.render() failure - check the game code. 
    at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:691) 
    at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:411) 
    at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:321) 
    at data.src.Core.main(Core.java:30) 
+0

讀圍繞如何從命令行運行並複製錯誤封郵件。它是一個小程序嗎?如果是這樣運行使用appletviewer和瀏覽器學習啓用Java控制檯,所以可以看到有錯誤信息 – tgkprog

+1

@sᴜʀᴇsʜᴀᴛᴛᴀ會做! – MrPork

+0

周長是參數? – tgkprog

回答

1

squareCorrectsquareIncorrect都爲空,因爲你不使用前設置的變量。

你應該剛剛宣佈他們之後加載它們:

public Image squareIncorrect; 
public Image squareCorrect; 
{ 
    squareIncorrect = new Image("res/squareCorrect"); 
    squareIncorrect = new Image("res/squareIncorrect"); 
} 
+0

非常感謝,它的工作!那麼,我現在學到了更多:P。這個網站應該認真支付你Njol。 – MrPork