我使用了www.raywenderlich.com的磁贴教程。我成立了一个横向视图瓦片地图和使用的代码从教程来检测触摸,就像这样:CCTMXTiledMap触摸检测有点关闭
CGPoint touchLocation = [touch locationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];
,并使用此位推测它是在哪个瓦片:
CGPoint touchTilePos = [self tileCoordForPosition:touchLocation];
int tileIndex = [self tileIndexForTileCoord:(touchTilePos)];
CCSprite *touchTile = [self.background tileAt:touchTilePos];
self.player.position = [touchTile.parent convertToWorldSpace:touchTile.position];
问题在于它有点偏离。触摸靠近左侧的位置非常靠近,但触摸靠近右侧的位置会被检测到向左移动。看起来像某种缩放问题,但我的代码中唯一的缩放比例是玩家精灵。
任何想法?建议? 任何halp非常感谢!
编辑:这是在代码中引用的方法有两种:
- (CGPoint) tileCoordForPosition:(CGPoint)position
{
int x = position.x/_tileMap.tileSize.width;
int y = ((_tileMap.mapSize.height * _tileMap.tileSize.height) - position.y)/_tileMap.tileSize.height;
return ccp(x, y);
}
- (int) tileIndexForTileCoord:(CGPoint)aTileCoord
{
return aTileCoord.y*(self.tileMap.mapSize.width) + aTileCoord.x;
}
更新: 我简化了代码:
CGPoint touchLocation = [touch locationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
touchLocation = [self.background convertToNodeSpace:touchLocation];
int x = touchLocation.x/_tileMap.tileSize.width;
int y = ((_tileMap.mapSize.height * _tileMap.tileSize.height) - touchLocation.y)/_tileMap.tileSize.height;
CGPoint touchTilePos = ccp(x,y);
另外,我注意到,马上开始,touchLocation是左边,我不认为这段代码对我的情况(iPad /横向)能正常工作:
CGPoint touchLocation = [touch locationInView: [touch view]];
upvoted回来并回答你自己的问题!对不起,我以前没有回复〜 – MechEthan 2012-04-30 17:04:27