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

 

 

 
最近读者:

登录后,您就出现在这里。 

转载于:https://www.cnblogs.com/wujian1360/archive/2012/03/10/2389613.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值