starwar2012 于 2013.06.26 14:19 提问

CCTMXTiledMap转换像素到网格

``````CGPoint mappos = [tileMapNode convertToNodeSpace:position];
mappos.x = (int) mappos.x / tileWidth;
mappos.y = (int) mappos.y / tileWidth;

CCTMXLayer *metaLayer = [tileMapNode layerNamed:@"Meta"];
CCSprite *metaTile = [metaLayer tileAt:ccp(mappos.x, mappos.y)];

if (metaTile)
{
NSLog(@"HIT!");
}
``````

2个回答

ce_la_vie   2013.06.26 16:05

``````#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define TILE_SIZE       ( IS_IPAD ? 64 : 32 )
#define TILE_IN_ROW     ( IS_IPAD ? 19 : 19 )
#define TILE_IN_COL     ( IS_IPAD ? 32 : 30 )
#define TILE_MAP_HEIGHT  (TILE_IN_COL*TILE_SIZE)
#define PP_TILE_META_LAYER      @"Meta"
#define PP_TILE_MAP_BG_LAYER    @"Background"
- (CGPoint)getTileCoordForPosition:(CGPoint)position
{
int maxTileCol = self.mapSize.height;
int x = ( (position.x-self.position.x)/TILE_SIZE);
int y = maxTileCol - ( ((position.y)-self.position.y)/TILE_SIZE);
if( x >= TILE_IN_ROW)
x = TILE_IN_ROW - 1;
if( y >= TILE_IN_COL)
y = TILE_IN_COL - 1;
return ccp(x, y);
}
mBgLayer = [self layerNamed:PP_TILE_MAP_BG_LAYER];
CGPoint point = [self getTileCoordForPosition:position];
CCSprite *sprite = [mBgLayer tileAt:point];
``````

xujiayin   2013.07.06 09:49

45度的要考验高中数学知识了，是一个二维坐标系变换问题。

2813610155