我有不同的图块层,我以不同的速度移动,我的播放器固定到位(水平)。现在,我如何检测到我的播放器是否与瓷砖相撞?我如何知道不同图层中图块的坐标?
答案 0 :(得分:3)
我在本教程http://paulsonapps.wordpress.com/2010/03/12/tutorial-1-tilemap-with-collision-game-cocos2d/
的帮助下解决了这个问题-(void)handleCollision
{
for (CCTMXLayer *lr in layers) {
// Determine the four corners of my player
int tlXright = floor(player.playerSprite.position.x+player.playerSprite.contentSize.width-lr.position.x);
int tlXleft = floor(player.playerSprite.position.x-lr.position.x);
int tlYup = floor(player.playerSprite.position.y+player.playerSprite.contentSize.height);
int tlYdown = floor(player.playerSprite.position.y);
//Convert our Map points
CGPoint nodeSpace1 = [tileMap convertToNodeSpace:ccp(tlXright,tlYdown)];
pos1X = floor(nodeSpace1.x / tileMap.tileSize.width);
pos1Y = floor(tileMap.mapSize.height - (nodeSpace1.y / tileMap.tileSize.height));
CGPoint nodeSpace2 = [tileMap convertToNodeSpace:ccp(tlXright,tlYup)];
pos2X = floor(nodeSpace2.x / tileMap.tileSize.width);
pos2Y = floor(tileMap.mapSize.height - (nodeSpace2.y / tileMap.tileSize.height));
CGPoint nodeSpace3 = [tileMap convertToNodeSpace:ccp(tlXleft,tlYdown)];
pos3X = floor(nodeSpace3.x / tileMap.tileSize.width);
pos3Y = floor(tileMap.mapSize.height - (nodeSpace3.y / tileMap.tileSize.height));
CGPoint nodeSpace4 = [tileMap convertToNodeSpace:ccp(tlXleft,tlYup)];
pos4X = floor(nodeSpace4.x / tileMap.tileSize.width);
pos4Y = floor(tileMap.mapSize.height - (nodeSpace4.y / tileMap.tileSize.height));
unsigned int gid5 = [lr tileGIDAt:ccp(pos1X,pos1Y)];
unsigned int gid6 = [lr tileGIDAt:ccp(pos2X,pos2Y)];
unsigned int gid7 = [lr tileGIDAt:ccp(pos3X,pos3Y)];
unsigned int gid8 = [lr tileGIDAt:ccp(pos4X,pos4Y)];
//NSLog(@"gid5:%d gid6:%d gid7:%d gid8:%d",gid5,gid6,gid7,gid8);
/ NSLog(@"pos1x:%d pos1y:%d pos4x:%d pos4y:%d",pos1X ,pos1Y,pos4X,pos4Y);
if (gid5 == 5) {
[lr removeTileAt:ccp(pos1X,pos1Y)];
}
if (gid6 == 5) {
[lr removeTileAt:ccp(pos2X,pos2Y)];
}
if (gid7 == 5) {
[lr removeTileAt:ccp(pos3X,pos3Y)];
}
if (gid8 == 5) {
[lr removeTileAt:ccp(pos4X,pos4Y)];
}
}
}
知道图块的坐标,我将图层位置减去播放器的位置
int tlXright = floor(player.playerSprite.position.x+player.playerSprite.contentSize.width-lr.position.x);
并在教程中转换它。
CGPoint nodeSpace1 = [tileMap convertToNodeSpace:ccp(tlXright,tlYdown)]; pos1X = floor(nodeSpace1.x / tileMap.tileSize.width); pos1Y = floor(tileMap.mapSize.height - (nodeSpace1.y / tileMap.tileSize.height));...