【发布时间】:2014-10-31 14:41:09
【问题描述】:
我正在用 Java 编写一个等距游戏,但是当我想获得鼠标指向的瓷砖时,我卡住了。
有办法计算吗?
这里是渲染代码
int posX = 10;
int posY = 0;
static int beginY = 500-72;
static int beginX = 800-36;
static int newLineY = 60;
static int newLineX = 200;
posY = ((0+1)*9)-9+beginY/2;
posX = ((beginX)/2)+18-(18*(0+1));
for (int y = 0; y <= world.length-1; y++) {
for (int x = 0; x <= world[y].length-1; x++) {
if (world[y][x] == 1) {
g.drawImage(grass, posX, posY, null);
posX += 18;
posY += 9;
} else if (world[y][x] == 2) {
g.drawImage(wall, posX, posY-38, null);
posX += 18;
posY += 9;
} else if (world[y][x] == 3) {
g.drawImage(stone, posX, posY, null);
posX += 18;
posY += 9;
} else if (world[y][x] == 4) {
g.drawImage(water, posX, posY, null);
posX += 18;
posY += 9;
} else {
posX += 18;
posY += 9;
}
if ((y-pPosX) * (y-pPosX) + (x-pPosY) * (x-pPosY) <= 3*3 && world[y][x] != 0 && world[y][x] != 2 && world[y][x] != 4) {
g.drawImage(hollow, posX-18, posY-9, null);
}
if (y == selectedX && x == selectedY) {
g.drawImage(selected, posX-18, posY-9, null);
}
if (world2[y][x] == 9) {
g.drawImage(character, posX-18, posY-53, null);
}
}
posY = ((y+1)*9)+beginY/2;
posX = ((beginX)/2)-(18*(y+1));
}
这就是结果。 beginX 和 beginY 从菱形的顶角开始:
【问题讨论】:
-
信息不够,请澄清。例如,您使用的是引擎、一些 openGL 实现还是基于摇摆的 GUI(使用 Canvas)?如果您可以添加一些代码,那将是最好的。你如何加载瓷砖?是图像还是直接绘制?
-
我不使用任何引擎,也不使用OpenGL,只使用JFrame。
-
我创建一个缓冲图像,然后定义它的路径。我插入代码。
标签: java cursor mouse isometric