为什么整个瓦片地图不显示?
Why wont the whole tile map show?
我正在尝试创建一个非常简单的瓷砖地图系统,几周前我遇到了问题并在这里询问,但最近我重写了它并且它停止正常工作。
请注意,我使用的是 slick2D,因此如果您想重现此内容,则必须将代码放入主渲染循环中。
数组
public static int[][] map = {{1,1,1,1,1,2,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,0,0,0,0,0,1}};
瓷砖地图循环。
int x = 0;
int y = 0;
int I = 0;
int II = 0;
while(y <= 7){
while( x <= 12){
if(map[y][x] == 0){
Image img = new Image("res/tile1.png");
img.draw(II,I);
}
if(map[y][x] == 1){
Image img = new Image("res/tile0.png");
img.draw(II,I);
}
if(map[y][x] == 2){
Image img = new Image("res/tile3.jpg");
img.draw(II,I);
}
x++;
II = x * 100;
}
y++;
I = y * 100;
}
截图http://puu.sh/iIf9r/42c3b6f4db.png
谢谢。
根据我从您的代码中了解到的情况,您喜欢将图像打印在 7x12 的矩形中。
如果我理解得很好,你必须在每一行之前重置 x,所以在 while( x <= 12){
之前
int x = 0;
int y = 0;
int I = 0;
int II = 0;
while(y <= 7){
x = 0;
while( x <= 12){
if(map[y][x] == 0){
Image img = new Image("res/tile1.png");
img.draw(II,I);
}
if(map[y][x] == 1){
Image img = new Image("res/tile0.png");
img.draw(II,I);
}
if(map[y][x] == 2){
Image img = new Image("res/tile3.jpg");
img.draw(II,I);
}
x++;
II = x * 100;
}
y++;
I = y * 100;
}
注意不需要每次都创建一个新图像来获得更好的性能。在所有之前创建图块并重复使用它们。
Davide 的回答似乎是正确的,但我想提请您注意您可以进行的一些优化。
当前在您的循环中,您正在检查您的图块是否等于某个值:
if(map[y][x] == 0)
...
if(map[y][x] == 1)
...
etc
这一切都很好,直到您有成百上千个图块。所有这些 if 语句都在做同样的事情,即加载平铺图像并绘制它。但是,出于速度目的,这不是主要问题。当您知道最终结果是什么时,主要问题之一是每次迭代都初始化图像。我不熟悉 slick2d
,但你可能会这样做:
// Initialize this once outside your render loop
Image[] tileImages = {
new Image("res/tile1.png"),
new Image("res/tile0.png"),
new Image("res/tile3.png")
};
...
int x = 0;
int y = 0;
int I = 0;
int II = 0;
while(y <= 7){
x = 0;
while( x <= 12){
// This replaces a lot of unnecessary code and makes it more efficient
tileImages[map[y][x]].draw(II, I);
x++;
II = x * 100;
}
y++;
I = y * 100;
}
注意:这个我没有测试过,但是大意是有的。另外,我把Davide关于设置x = 0
值的原始代码用这个方法修改了。
我正在尝试创建一个非常简单的瓷砖地图系统,几周前我遇到了问题并在这里询问,但最近我重写了它并且它停止正常工作。
请注意,我使用的是 slick2D,因此如果您想重现此内容,则必须将代码放入主渲染循环中。
数组
public static int[][] map = {{1,1,1,1,1,2,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,0,0,0,0,0,1}};
瓷砖地图循环。
int x = 0;
int y = 0;
int I = 0;
int II = 0;
while(y <= 7){
while( x <= 12){
if(map[y][x] == 0){
Image img = new Image("res/tile1.png");
img.draw(II,I);
}
if(map[y][x] == 1){
Image img = new Image("res/tile0.png");
img.draw(II,I);
}
if(map[y][x] == 2){
Image img = new Image("res/tile3.jpg");
img.draw(II,I);
}
x++;
II = x * 100;
}
y++;
I = y * 100;
}
截图http://puu.sh/iIf9r/42c3b6f4db.png
谢谢。
根据我从您的代码中了解到的情况,您喜欢将图像打印在 7x12 的矩形中。
如果我理解得很好,你必须在每一行之前重置 x,所以在 while( x <= 12){
int x = 0;
int y = 0;
int I = 0;
int II = 0;
while(y <= 7){
x = 0;
while( x <= 12){
if(map[y][x] == 0){
Image img = new Image("res/tile1.png");
img.draw(II,I);
}
if(map[y][x] == 1){
Image img = new Image("res/tile0.png");
img.draw(II,I);
}
if(map[y][x] == 2){
Image img = new Image("res/tile3.jpg");
img.draw(II,I);
}
x++;
II = x * 100;
}
y++;
I = y * 100;
}
注意不需要每次都创建一个新图像来获得更好的性能。在所有之前创建图块并重复使用它们。
Davide 的回答似乎是正确的,但我想提请您注意您可以进行的一些优化。
当前在您的循环中,您正在检查您的图块是否等于某个值:
if(map[y][x] == 0)
...
if(map[y][x] == 1)
...
etc
这一切都很好,直到您有成百上千个图块。所有这些 if 语句都在做同样的事情,即加载平铺图像并绘制它。但是,出于速度目的,这不是主要问题。当您知道最终结果是什么时,主要问题之一是每次迭代都初始化图像。我不熟悉 slick2d
,但你可能会这样做:
// Initialize this once outside your render loop
Image[] tileImages = {
new Image("res/tile1.png"),
new Image("res/tile0.png"),
new Image("res/tile3.png")
};
...
int x = 0;
int y = 0;
int I = 0;
int II = 0;
while(y <= 7){
x = 0;
while( x <= 12){
// This replaces a lot of unnecessary code and makes it more efficient
tileImages[map[y][x]].draw(II, I);
x++;
II = x * 100;
}
y++;
I = y * 100;
}
注意:这个我没有测试过,但是大意是有的。另外,我把Davide关于设置x = 0
值的原始代码用这个方法修改了。