단순하지만 강력한 충돌 탐지를 위해서는 물리 엔진이 필요할 것입니다. Chipmunk 엔진 용 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);
물론 다른 일부 al 충돌을 감지하는 고딕체이지만, 이것은 tmx에서이 정보를 저장하고 검색하는 예제 일뿐입니다.
Cocos2d는 retina tmx를 지원하는 버그가 있습니다. 타일 맵의 HD 버전을 사용하는 경우 좌표와 치수를 2로 나눈 모든 객체를 검색해야합니다. –