www.raywenderlich.comのタイルチュートリアルを使用しました。横向きのビューでタイルマップを設定し、チュートリアルのコードを使用して、次のようにタッチを検出しました。
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];
問題はそれが少しずれていることです。左側に近いタッチはかなり近いですが、右側に近いタッチは左側から離れて検出されます。ある種のスケーリングの問題のように見えますが、私のコードの唯一のスケーリングはプレーヤーのスプライトです。
何か案は?提案?どんな半分でも大歓迎です!
編集:コードで参照されている2つのメソッドは次のとおりです。
- (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]];