开发者

java - loading different maps

OK so I have this applet I am making and I want it to generator a world according to a number...

Here it is:

 public int[][] loadBoard(int map) {
    if (map == 1) { int[][] board = { {
 2,2,24,24,24,24,24,3,3,0,0,0,1 },

 { 2,2,24,23,23,23,24,1,3,0,0,0,1 },

 { 1,1,24,23,23,23,24,1,3,3,3,3,1 },

 { 1,1,24,24,23,24,24,1,1,1,1,3,1 },

 { 1,1,1,1,7,1,1,1,1,1,1,3,1 },

 { 5,1,1,1,7,7,7,7,7,1,1,1,1 },

 { 6,3,3,1,3,3,3,1,7,7,7,3,1 },

 { 6,3,3,1,3,1,1,1,1,1,7,1,1 },

 { 3,3,1,1,1,1,1,1,1,1,7,1,1 } };

 }else{

 int[][] board = {

 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },


 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,24,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,24,1,1,1,1 },

 { 1,1,7,1,1,24,24,24,24,1,1,1,1 },

 { 1,1,7,1,1,24,1,24,1,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },

 { 1,3,3,1,1,24,1,1,1,1,1,1,1 },

 }; } return board; }

and to call it I use:

board = loadBoard(1);

I put that in the init() method. Then that way I can call maps upon the number inside loadBoard()开发者_C百科. However, when I start my game I get nullpointer exception and I KNOW for a FACT that its something to do with the code I just showed you above. It's probably some rookie mistake I am doing.. maybe you can help?


It is. You create "board" variable again. Even though the name is the same the variable you return is not the one you created. Here is the fixed code:

public int[][] loadBoard(int map) {
    if (map == 1) { 
 return new int[][] { 

 {2,2,24,24,24,24,24,3,3,0,0,0,1 },

 { 2,2,24,23,23,23,24,1,3,0,0,0,1 },

 { 1,1,24,23,23,23,24,1,3,3,3,3,1 },

 { 1,1,24,24,23,24,24,1,1,1,1,3,1 },

 { 1,1,1,1,7,1,1,1,1,1,1,3,1 },

 { 5,1,1,1,7,7,7,7,7,1,1,1,1 },

 { 6,3,3,1,3,3,3,1,7,7,7,3,1 },

 { 6,3,3,1,3,1,1,1,1,1,7,1,1 },

 { 3,3,1,1,1,1,1,1,1,1,7,1,1 } };

 }else{

 return new int[][] {

 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },


 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,24,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,24,1,1,1,1 },

 { 1,1,7,1,1,24,24,24,24,1,1,1,1 },

 { 1,1,7,1,1,24,1,24,1,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },

 { 1,3,3,1,1,24,1,1,1,1,1,1,1 },

 }; } 

}

It can be simp'ified even more if I would now how many "numbers" you're planing to have :)

Another suggestion would be to NOT create arrays on the fly, but have them as constants. Then return appropriate array from the method. Your code may look like (for more then 2 choices):

 private static final int[][] BOARD1 = <array here>;
 private static final int[][] BOARD2 = <array here>;
 private static final int[][] BOARD3 = <array here>;
 private static final int[][] BOARD4 = <array here>;


 public function int[][] loadBoard( int choice ) {
   switch( choice ) {
      case 1: return BOARD1;
      case 2: return BOARD2;
      case 3: return BOARD3;
      case 4: return BOARD4;
      default: throw new RuntimeException( "Unknown board choice" );
   }
 }


I'm not sure that your null pointer is related to this code. However, it is really weird the fact that you declare board inside the scope of the if/else statement and the return this variable outside the scope.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜