2010-04-08 3 views
2

나는 Cocos2D를 사용하여 appstore의 Flight Control, Harbor Master 및 다른 것과 비슷한 선 그리기 게임을 개발 중입니다.CGPoints의 연속을 따르는 CCSprite

이 게임에서는 사용자가 그린 선을 따라 CCSprite가 필요합니다. 내가 touchesBegintouchesMoved 메시지를 얻을 포인트에 따라 NSArrayCGPoint 구조체의 시퀀스를 저장 해요. 나는 이제 내 스프라이트를 어떻게 따라야하는지에 대한 문제를 가지고있다.

나는 프레임 속도로 불리는 진드기 방법을 사용합니다. Sprite의 속도와 현재 위치를 기반으로하는 tick 메서드에서 다음 위치를 계산해야합니다. 이것을 달성하기위한 표준 방법이 있습니까?

내 현재 접근법은 마지막 "참조 점"과 다음 점 사이의 선을 계산하고 그 선에서 다음 점을 계산합니다. 내가 가진 문제는 스프라이트가 "회전"(줄의 한 세그먼트에서 다른 세그먼트로 이동) 할 때입니다.

모든 힌트를 크게 높이 실 것입니다.

답변

6

왜 자신의 틱 방식을 코딩합니까? 왜 내장 된 CCMoveTo 메서드를 사용하지 않으시겠습니까?

(void) gotoNextWayPoint { 
    // You would need to code these functions: 
    CGPoint point1 = [self popCurrentWayPoint]; 
    CGPoint point2 = [self getCurrentWayPoint]; 

    // Calculate distance from last way point to next way point 
    CGFloat dx = point2.x - point1.x; 
    CGFloat dy = point2.y - point1.y; 
    float distance = sqrt(dx*dx + dy*dy); 

    // Calculate angle of segment 
    float angle = atan2(dy, dx); 

    // Rotate sprite to angle of next segment 
    // You could also do this as part of the sequence (or CCSpawn actually) below 
    // gradually as it approaches the next way point, but you would need the 
    // angle of the line between the next and next next way point 
    [mySprite setRotation: angle]; 

    CCTime segmentDuration = distance/speed; 

    // Animate this segment, and afterward, call this function again 
    CCAction *myAction = [CCSequence actions: 
      [CCMoveTo actionWithDuration: segmentDuration position: nextWayPoint], 
      [CCCallFunc actionWithTarget: self selector: @selector(gotoNextWayPoint)], 
      nil]; 

    [mySprite runAction: myAction]; 
} 
관련 문제