첨엔 귀찮아서 걍 사각형으로 충돌체크 했다가 민원 들어오길래 후후 고쳐본..
The only interesting question in this chapter is how to convert mouse coordinates from the screen to the tiles so we know which tile player has clicked. As you might remember from the previous chapter, we used:
game.xmouse=Math.round((_root._xmouse-game.tileW/2)/game.tileW); game.ymouse=Math.round((_root._ymouse-game.tileH/2)/game.tileH);
If you ever wondered why we used those lines, then no, its not because the code looks good and is long enough to impress your girlfriend. The reason we used that code was mainly because we had placed the hero on correct place using lines:
char.x = (char.xtile *game.tileW)+game.tileW/2; char.y = (char.ytile *game.tileW)+game.tileW/2;
Dont be shy, look at the two pairs. I can even rewrite the lines for clarity. Lets take the code with char.x, if we replace strange names with simple letters, it says:
a=b*c+d
Now for us to find mouse coordinates, we need to get letter "b" from that equation:
b=(a-d)/c
and this is exactly, what we have used for mouse. OK, but we were here to talk about isometric. In isometric view we cant get the tile clicked with mouse using same code because we have placed tiles in different way. All the tiles are placed in isometric using code:
xiso=x-y yiso=(x+y)/2
In order to find out, what tile has been clicked, we need to find variables "x" and "y" from those equations. Lets rewrite first line:
x=xiso+y
Now replace the equation for x into second line:
yiso=(xiso+y+y)/2
which can be rewritten couple of times:
yiso=(xiso+2*y)/2 2*yiso=xiso+2*y 2*y=2*yiso-xiso y=(2*yiso-xiso)/2
And we have created two lines to calculate tile in isometric space from the screen coordinates:
y=(2*yiso-xiso)/2 x=xiso+y
Actual code
In the work function use this code to find out the isometric tile under the mouse:
var ymouse=((2*game.clip._ymouse-game.clip._xmouse)/2); var xmouse=(game.clip._xmouse+ymouse); game.ymouse=Math.round(ymouse/game.tileW); game.xmouse=Math.round(xmouse/game.tileW)-1;
Im sure you can see the similarities with 2 lines we created before. Variables xmouse and ymouse have values where mouse would have been, if we wouldnt be silly enough to start with all this isometric stuff.
Remember, dont use _root._xmouse because our "tiles" movie clip was moved (game.clip._x=150) and we want to get mouse inside "tiles" movie clip. If you use _root._xmouse, then the tile with x=0 would be
placed in the left side of the stage, but isometric view places that tile about in the center of stage.
충돌 체크까진 아니고 현재 마우스 위치를 나눠서 col,row를 찾는다.
계산 다하고 난후 x가 0보다 크고 max_col보다 작아야 하는지 체크하고
y가 0보다 크고 max_row보다 작은지 체크한뒤에 그놈의 인덱싱 저장후
currentCol = y; currentRow = x;로 뒤바꿔 저장해주면 깔끔~~~
나를위하여 : 혹시나 까먹을 시에는 맵툴의 FindMouseToMapIndex 메소드의 주석부분을 보시오.
후 쿼터뷰는 진짜 짱나는군.. 쩝.. 돌아버리겠다는. 아우.....