java - 加载不同的地图
好的,我正在制作这个小程序,我希望它根据数字生成一个世界...
它是:
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; }
并调用它我使用:
board = loadBoard(1);
我把它放在 init() 方法中。这样我就可以根据 loadBoard() 内的数字调用地图。然而,当我开始游戏时,我得到了空指针异常,并且我知道它与我上面刚刚向您展示的代码有关。这可能是我正在犯的一些菜鸟错误..也许你可以帮忙?
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(). 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是。您再次创建“board”变量。即使名称相同,您返回的变量也不是您创建的变量。这是固定的代码:
如果我现在知道您计划拥有多少个“数字”,它可以进一步简化:)
另一个建议是不要动态创建数组,而是将它们作为常量。然后从该方法返回适当的数组。您的代码可能如下所示(对于两个以上的选择):
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:
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):
我不确定你的空指针与这段代码有关。然而,在 if/else 语句的作用域内声明
board
并在作用域外返回此变量的事实确实很奇怪。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.