2013-04-25 2 views
0

안녕하세요. 스택 오버플로가 좋은 사람들 오늘 (아주 좋지 않은) cocos2d 문제로 내가 서 있습니다.CCMoveTo 동안 타일 된 타일 맵의 충돌을 지속적으로 확인하는 방법

대상 타일을 확인하는 대신 작업에서 충돌을 어떻게 확인할 수 있습니까? 충돌 가능한 타일이있는 메타 레이어가있는 타일 맵으로 작업하고 있습니다. 다음 코드는 충돌 가능한 타일을 클릭 할 때 작동하지만 넘어서 클릭 할 때 작동하지 않습니다.

는 현재,이 코드와 충돌을 감지 :

-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
[_player stopAllActions]; 
CGPoint touchLocation = [touch locationInView:touch.view]; 

touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation]; 
touchLocation = [self convertToNodeSpace:touchLocation]; 

CGPoint playerPos = _player.position; 
CGPoint diff = ccpSub(touchLocation, playerPos); 

int diffx = diff.x; 
int diffy = diff.y; 
int adiffx = diffx/24; 
int adiffy = diffy/24; //Porque my tile size is 24  

if (abs(diff.x) > abs(diff.y)) { 
    if (diff.x > 0) { 
     playerPos.x = playerPos.x + adiffx * 24; 
    } else { 
     playerPos.x = playerPos.x + adiffx * 24; 
    } 
} else { 
    if (diff.y > 0) { 
     playerPos.y = playerPos.y + adiffy * 24; 
    }else{ 
     playerPos.y = playerPos.y + adiffy * 24; 
    } 
} 
[self setPlayerPosition:playerPos]; 
} 

내가 운이없이

[self schedule:@selector(setPlayerPosition:) interval:0.5]; 

을 시도 :

-(void)setPlayerPosition:(CGPoint)position { 
CGPoint tileCoord = [self tileCoordForPosition:position]; 
int tileGid = [_meta tileGIDAt:tileCoord]; 
if (tileGid) { 
    NSDictionary *properties = [_tileMap propertiesForGID:tileGid]; 
    if (properties) { 
     NSString *collision = properties[@"Collidable"]; 
     if (collision && [collision isEqualToString:@"True"]) { 
      return; 
     } 
    } 
} 

float speed = 10; 
CGPoint currentPlayerPos = _player.position; 
CGPoint newPlayerPos = position; 
double PlayerPosDifference = ccpDistance(currentPlayerPos, newPlayerPos)/24; 
int timeToMoveToNewPostition = PlayerPosDifference/speed; 
id moveTo = [CCMoveTo actionWithDuration:timeToMoveToNewPostition position:position]; 
[_player runAction:moveTo]; 

//_player.position = position; 
} 

그런데, 이것은이 방법에서 호출되는 앱이 즉시 앱을 즉시 추락 시켰습니다.

NSAssert(pos.x < _layerSize.width && pos.y < _layerSize.height && pos.x >=0 && pos.y >=0, @"TMXLayer: invalid position"); 그리고 나는 그곳에서 충돌 한 것을 비난 할 수는 없습니다.

CCMoveTo이 실행되는 동안 지속적으로 충돌 가능한 메타 타일과의 충돌을 확인하려면 어떻게해야합니까?

+0

당신의 초기화에 –

+0

아니, 내가 어떻게하는지 모른다 : D 당신은 나에게 계몽 할 수 있을까? @SmugbitStudios –

답변

0

는, 충돌 감지 방법을 예약합니다. 모든 타일 배열을 유지합니까? 그렇다면 플레이어와 모든 타일 간의 충돌을 적극적으로 확인하는 "충돌 감지기"방법을 예약 할 수 있습니다.
+0

정말 고마워! 이 (일종의) 작품!그러나 그는 그 (것)들과 충돌 할 때 도와 안쪽으로 찔리고. 그의 방법으로 무언가가있을 때 그를 걸을 수없는 어떤 방법? –

+0

이렇게하면 스프라이트가 벽 안쪽에있는 경우에만 스프라이트가 멈 춥니 다. – dqhendricks

+0

이동하기 전에 충돌을 확인하고 이동으로 벽 안으로 들어가게되면 이동을 중지해야합니다. – dqhendricks

-1

비슷한 메타 레이어를 사용하고 있습니다. 저는 개인적으로 MoveTo를 사용하지 않고 스프라이트를 움직이고 충돌 감지를 수행하는 자체 업데이트 코드를 만들었습니다.

가능성있는 새로운 위치가 무엇인지 파악한 다음 스프라이트의 네 모서리에있는 타일 위치가 새 위치에 있는지 알아 본 다음 스프라이트가있을 모든 타일을 순환합니다. collidable, 나는 스프라이트를 멈춘다.

사실 저는 한 걸음 더 나아가 x 만 움직이거나 y 만 결과가 바뀌는 지 확인한 다음 그 위치로 이동합니다. 다음과 같이

[self schedule:@selector(checkCollisions:)]; 

이 그 다음을 정의합니다 : 당신은 터치의 위치에서 타일을 확인하고

-(void)checkCollisions:(ccTime)dt 
{ 
    CGPoint currentPlayerPos = _player.position; 
    CGPoint tileCoord = [self tileCoordForPosition:currentPlayerPos]; 
    int tileGid = [_meta tileGIDAt:tileCoord]; 
    if (tileGid) 
    { 
     NSDictionary *properties = [_tileMap propertiesForGID:tileGid]; 
     if (properties) 
     { 
      NSString *collision = properties[@"Collidable"]; 
      if (collision && [collision isEqualToString:@"True"]) 
      { 
       [_player stopAllActions]; 
       return; 
      } 
     } 
    } 
} 
+0

이것은 똑똑해 보이지만, 나는 내 자신의 CCMoveTo 메소드를 구현할 수준이 아니다. 예제를 제공 할 수 있습니까? 밖으로 시작하는 방법에 조금 더 indepth? –

+0

나는 그것을 간단하게 유지하지만 하나의 복잡한 수학 방정식을 사용한다. 대신 foo 위치로 이동을 말하면서, 나는 foo 방향으로 움직이고, bar의 속도로 말합니다. 그런 다음 수학을 사용하여 다음 프레임 위치가 방향과 속도를 기반으로 할 위치를 파악합니다. 새로운 위치가 무엇인지 알게되면 위에서 언급 한 내용을 확인할 수 있습니다. 또한,이 움직임을 제한하기 위해 바의 속도로 바의 속도로 foo 방향으로 이동한다고 말합니다. 그런 다음 총 경과 시간을 추적하고 설정된 지속 시간이 끝나면 이동을 중지합니다. – dqhendricks

+0

똑똑한 것처럼 보입니다. 코드 게시에 관심이 있으십니까? 제발 : D –