cocos2d 环绕已知点移动一圈

 


答案1:

问题解决 
将step改为如下即可
-(void)step:(ccTime)dt
{
    if (rotate == 360) {
        rotate = 0;
    }
    rotate += 5;
    float fradian = rotate * PI / 180;
    NSLog(@"%f  %f",rotate,fradian);
    id ac0 = [CCMoveTo actionWithDuration:0.01 
                                 position:*****(centerx + radius * sinf(fradian),
                                              centery + radius * cosf(fradian))];
    id ac1 = [CCRotateTo actionWithDuration:0.01 angle:(int)(rotate)];
    [sparrowhead runAction:[CCSpawn actions:ac0,ac1,nil]];
}

答案2:

根据算法,写成一个cocos2d的action

 
  1. /** Round circle a CCNode object clockwise a number of degrees by modiying it's rotation attribute.
  2. */
  3. @interface CCRoundBy : CCActionInterval <NSCopying>
  4. {
  5.     BOOL turn;// Forward or Reverse round
  6.     float startAngle;// default
  7.     float radius;// Round circle radius
  8.     CGPoint center;// Round circle center point
  9. }
  10. /** creates the action */
  11. +(id) actionWithDuration:(ccTime)duration turn:(BOOL)a center:(CGPoint)point radius:(float)r;
  12. /** initializes the action */
  13. -(id) initWithDuration:(ccTime)duration turn:(BOOL)a center:(CGPoint)point radius:(float)r;
  14. @end
 
  1. //
  2. // CCRoundBy
  3. //
  4. #pragma mark -
  5. #pragma mark CCRoundBy
  6.  
  7. @implementation CCRoundBy
  8. +(id) actionWithDuration: (ccTime) t turn:(BOOL) a center:(CGPoint)point radius:(float)r
  9. {    
  10.     return [[[self alloc] initWithDuration:t turn:a center:point radius:r] autorelease];
  11. }
  12.  
  13. -(id) initWithDuration: (ccTime) t turn:(BOOL) a center:(CGPoint)point radius:(float)r
  14. {
  15.     if( (self=[super initWithDuration: t]) ) {
  16.         turn = a;
  17.         radius = r;
  18.         center = point;
  19.     }
  20.     return self;
  21. }
  22.  
  23. -(id) copyWithZone: (NSZone*) zone
  24. {
  25.     CCAction *copy = [[[self class] allocWithZone: zone] initWithDuration: [self duration] turn: turn center:center radius:radius];
  26.     return copy;
  27. }
  28.  
  29. -(void) startWithTarget:(id)aTarget
  30. {
  31.     [super startWithTarget:aTarget];
  32.     startAngle = [target_ rotation];
  33.     if (turn) {
  34.         ((CCNode *)target_).position = *****Add(center, *****(-radius, 0));
  35.     }
  36.     else {
  37.         ((CCNode *)target_).position = *****Add(center, *****(radius, 0));
  38.     }    
  39. }
  40.  
  41. -(void) update: (ccTime) t
  42. {    
  43.     // XXX: shall I add % 360
  44.     float rotate =  (startAngle + 360.0f * t );
  45.     if (turn) {
  46.         rotate *= -1;
  47.     }
  48.     [target_ setRotation:rotate];
  49.     
  50.     float fradian = rotate * M_PI / 180.0f;
  51.     CGPoint pos = *****(center.x + radius * sinf(fradian),
  52.                       center.y + radius * cosf(fradian));
  53.     [target_ setPosition: pos];
  54. }
  55.  
  56. -(CCActionInterval*) reverse
  57. {    
  58.     BOOL result = !turn;
  59.     return [[self class] actionWithDuration:duration_ turn:result center:center radius:radius];
  60. }
  61.  
  62. @end

 

 

 
最近读者:

登录后,您就出现在这里。  
posted @ 2012-03-10 22:48  wujian1360  阅读(657)  评论(0编辑  收藏  举报