对于简单但功能强大的碰撞检测,您最有可能需要物理引擎。 我推荐Chipmank引擎的SpaceManager扩展。
在Tiled中,您可以将对象图层添加到tmx文件,您可以在其中排列所有障碍物。 这就是我在我的游戏场景解析这样的信息:
self.tileMap = [CCTMXTiledMap tiledMapWithTMXFile:@"TileMap.tmx"];
...
smgr = [[SpaceManagerCocos2d alloc] init];
smgr.constantDt = 1/60.0;
smgr.gravity=ccp(0, 0);
[smgr addWindowContainmentWithFriction:0.0 elasticity:0.0 size:CGSizeMake(1000, 1000) inset:cpvzero radius:1.0f]; //borders of your world, can be set from tmx size
player = [smgr addCircleAt:ccp(x,y) mass:6 radius:15];
和地方:
在初始化
- (void) drawWalls {
//searching for object layer called "Collisions"
CCTMXObjectGroup *objects = [_tileMap objectGroupNamed:@"Walls"];
NSMutableDictionary * objPoint;
int x ;
int y ;
int w ;
int h ;
for (objPoint in [objects objects]) {
x = [[objPoint valueForKey:@"x"] intValue];
y = [[objPoint valueForKey:@"y"] intValue];
w = [[objPoint valueForKey:@"width"] intValue];
h = [[objPoint valueForKey:@"height"] intValue];
GameController *controller = [GameController sharedController];
if(controller.retina==1)
{
x=x/2;
y=y/2;
w=w/2;
h=h/2;
}
cpShape *staticShape = [smgr addRectAt:ccp(x+w/2,y+h/2) mass:STATIC_MASS width:w height:h rotation:0];
}
}
然后,您可以在“玩家”对象施加外力和更新在“更新”方法中的精灵位置:
playerSprite.position=ccp(player->body->p.x, player->body->p.y);
当然,你可以使用其他的一些算法来检测碰撞,但这只是从tmx存储和检索这些信息的一个例子。
Cocos2d有一个视网膜tmx支持的错误。如果您使用的是高清版本的瓦片地图,则应该检索坐标和维度除以2的所有对象。 –