달력

10

« 2024/10 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
http://www.cocos2d-iphone.org/forum/topic/7927

That's a fun one... I had to sit down with some graph paper and math and spend a while working it out, I wanted a simple solution that would work in all cases no matter the map size. Hope this saves you some time.

I use multiple ISO map sizes and the touchToXY is in my base map class, it works no matter the map size it will always return the X,Y. Top most tile of the diamond being 0,0.

Here is my solution:

-(CGPoint)touchToXY:(CGPoint)touch map:(CCTMXTiledMap *)_map mapsize:(int)_mapsize
{
	touch = [[CCDirector sharedDirector] convertToGL:touch];
	touch = [_map convertToNodeSpace:touch];
	CGSize ts = [_map tileSize];
	int isoy = (((touch.y/ ts.height) + (touch.x - (_mapsize * ts.width/2)) / ts.width) - _mapsize) *-1;
	int isox = (((touch.y/ ts.height) - (touch.x - (_mapsize * ts.width/2)) / ts.width) -_mapsize) *-1;
	return ccp(isox,isoy);
}

To call it:

Variables you need to define are

<CCTMXTiledMap> = Your Tile Map
<MapWidthInTiles> = How big is the grid.  20 if it's 20x20, 30 if it's 30x30 etc..

Call from your touch code:

CGPoint myTouch = [touch locationInView: [touch view]];
CGPoint isoXY = [self touchToXY:myTouch map:<CCTMXTiledMap> mapsize:<MapWidthInTiles>];
Posted 2 months ago #

:
Posted by netkorea