0

나는 cocos2d를 사용하여 iPhone 게임을 만들려고합니다. 주어진 경로로 볼을 굴려서 특정 물체를 만질 수 없습니다.가속도계로 플레이어가 움직이는 타일 세트 콜리 더를 만드는 법 ios, cocos2d

저는 타일 콜리더를 만드는 방법을 가르쳐 준 RPG 튜토리얼을 작성했으며 isTouchEnabled를 사용하여 공을 움직일 때 작동합니다. 그러나 가속도계에 동일한 로직을 적용하면 등록하지 않는 것 같습니다.

이것은 터치 플레이어 이동 방법이므로, 어떻게 작동하는지 볼 수 있습니다. 다음은

 -(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    CGPoint touchLocation = [touch locationInView:[touch view]]; 
    touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation]; 
    touchLocation = [self convertToNodeSpace:touchLocation]; 

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

    //move horizontal or vertical? 
    if(abs(diff.x) > abs(diff.y)){ 
     if(diff.x > 0) 
     { 
      playerPos.x += theMap.tileSize.width; 
     }else{ 
      playerPos.x -= theMap.tileSize.width; 
     } 
    }else{ 
     if(diff.y > 0){ 
      playerPos.y += theMap.tileSize.height; 
     }else{ 
      playerPos.y -= theMap.tileSize.height; 
     } 


    } 

    //make sure the player isnt off the map 
    if(playerPos.x <= (theMap.mapSize.width * theMap.tileSize.width) && 
     playerPos.y <= (theMap.mapSize.height * theMap.tileSize.height) && 
     playerPos.x >=0 && 
     playerPos.y >=0) 
    { 
     [self setPlayerPostion:playerPos]; 
    } 
    [self setCenterOfScreen:ball.position]; 
} 

는 공의 위치를 ​​얻을 가속도계 여기

-(void)updateBall:(ccTime)delta{ 

//ball.position = ccp(ball.position.x, ball.position.y + tiltVertical); 
//ball.position = ccp(ball.position.x + tiltHorizontal, ball.position.y); 


CGPoint playerPos = ball.position; 

playerPos.x += tiltHorizontal; 
playerPos.y += tiltVertical; 

ball.position = playerPos; 
//make sure the player isnt off the map 
if(playerPos.x <= (theMap.mapSize.width * theMap.tileSize.width) && 
    playerPos.y <= (theMap.mapSize.height * theMap.tileSize.height) && 
    playerPos.x >=0 && 
    playerPos.y >=0) 
{ 
    ball.position = playerPos; 
    CCLOG(@"testing for off screen"); 
    [self setPlayerPostion:playerPos]; 
    ball.position = playerPos; 
} 

ball.position = playerPos; 
[self setCenterOfScreen:ball.position]; 

} 

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration  *)acceleration{ 

tiltHorizontal = (acceleration.y* (-20)); 
tiltVertical = (acceleration.x) * 20; 


} 

를 사용하여 이동 타일의 위치를 ​​받고보고에 사용되는 두 가지 방법이 어떻게 타일의 속성 경우 Collidable 및 True (지도를 만들 때 타일로 표시됨)입니다.

-(void)setPlayerPostion:(CGPoint)position{ 
CGPoint tileCoord = [self tileCoordForPosition:position]; 

int tileGid = [stLayer tileGIDAt:tileCoord]; 

if(tileGid) 
{ 
    NSDictionary *properties = [theMap propertiesForGID:tileGid]; 

    if(properties){ 
     NSString *collision = [properties valueForKey:@"Collidable"]; 
     if(collision && [collision compare:@"True"] ==NSOrderedSame){ 
      return; 
     } 
    } 
} 
ball.position = position; 
} 

-(CGPoint)tileCoordForPosition:(CGPoint)position{ 
    int x = position.x/theMap.tileSize.width; 
    int y = ((theMap.mapSize.height * theMap.tileSize.height)- position.y)/theMap.tileSize.height; 
    return ccp(x,y); 
    } 

내 논리에 문제가 있습니까? 또는 어떤 제안이라도 인정 될 것입니다.

감사

답변

0

당신이 당신의 init 메소드의 라인 self.isAccelerometerEnabled = YES이 있는지 확인합니다.

+0

네, 그걸 가지고 있습니다. 볼은 내지도를 굴러 다니고, 충돌 가능한 타일을 감지하지 못합니다. – Peerkon

+0

단계별 디버거를 추가 했습니까? '속성'에 문제가 있습니까? – tams

관련 문제