Saturday 15 May 2010

cursor - Java diamond-shape isometric map tile pick -



cursor - Java diamond-shape isometric map tile pick -

i'm programming isometric game in java, stuck when wanted tile, mouse pointing.

is there way, calculate it?

here code rendering

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)); (int y = 0; y <= world.length-1; y++) { (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)); }

this result. beginx , beginy start @ top corner of diamond:

first register mouselistener panel (the panel want draw tiles). now, according rendering code "know" each tile starts. have calculate tile clicked.

jpanel gamepanel = new jpanel(); gamepanel.addmouselistener(new mouseadapter() { @override public void mouseclicked(mouseevent event) { int x=event.getx(); int y=event.gety(); system.out.println("clicked @ ("+x+","+y+")"); //these co-ords relative gamepanel } });

edit:

i changed code above, wasn't correct. sure you'll figure out. above code shows how add together mouselistener panel/frame. , if works, should able see in console click.

now have calculate tile clicked:

(x,y) --> + - - - ' /\ ' / \ '/ \ \ / \ / \/ + <-- (x+36, y+18)

your mouse pointer needs somewhere between 2 points (x,y) , (x+36, y+18). of course of study there additional problem click somewhere between (x,y) , (y+36, y+18) still not click on tile (maybe clicks on tile top left of it) , there may need decide tile want selected.

another way split panel panel of squares. without drawing them of course. imagine each tile have square in middle:

/\ /____\ / | | \ \ |____| / \ / \/

something that. need calculate if clicked within square.

java cursor mouse isometric

No comments:

Post a Comment