cocos2d ccsprite 偏心转动

做了点儿格式上的小改动,写了一个小用例,逻辑还是原作的逻辑~

原文链接:http://www.cocoachina.com/bbs/simple/?t40047.html

让 ccsprite 围绕一个点做旋转运动,cocos2d 本身的 setAnchorPoint 虽然可以,

但是 sprite 本身也在自转着,有时后我们不需要 sprite 自转,这时候下面的扩展便能派上用场了~

BYRoundBy.h

//
//  BYRoundBy.h
//  Oh my fish!
//
//  Created by Bruce Yang on 12-8-9.
//  Copyright (c) 2012年 EricGameStudio. All rights reserved.
//


/**
 * 参考链接:
 * http://www.cocoachina.com/bbs/simple/?t40047.html
 */
#import "cocos2d.h"

/**
 * Round circle a CCNode object clockwise a number of degrees by modiying it's rotation attribute.
 */
@interface BYRoundBy : CCActionInterval <NSCopying> {
    // Forward or Reverse round
    BOOL turn;
    
    // default
    float startAngle;
    
    // Round circle radius
    float radius;
    
    // Round circle center point
    CGPoint center;
}

/**
 * creates the action~
 */
+(id) actionWithDuration:(ccTime)duration turn:(BOOL)a center:(CGPoint)point radius:(float)r;


/**
 * initializes the action~
 */
-(id) initWithDuration:(ccTime)duration turn:(BOOL)a center:(CGPoint)point radius:(float)r;


@end


BYRoundBy.mm

//
//  BYRoundBy.mm
//  Oh my fish!
//
//  Created by Bruce Yang on 12-8-9.
//  Copyright (c) 2012年 EricGameStudio. All rights reserved.
//

#import "BYRoundBy.h"

//
// CCRoundBy
//
#pragma mark -
#pragma mark BYRoundBy

@implementation BYRoundBy

+(id) actionWithDuration:(ccTime)t turn:(BOOL)a center:(CGPoint)point radius:(float)r {    
    return [[[self alloc] initWithDuration:t turn:a center:point radius:r] autorelease];
}

-(id) initWithDuration:(ccTime)t turn:(BOOL)a center:(CGPoint)point radius:(float)r {
    if((self = [super initWithDuration:t])) {
        turn = a;
        center = point;
        radius = r;
    }
    return self;
}

-(id) copyWithZone:(NSZone*)zone {
    CCAction* copy = [[[self class] allocWithZone: zone] initWithDuration: [self duration] turn:turn center:center radius:radius];
    return copy;
}

-(void) startWithTarget:(id)aTarget {
    [super startWithTarget:aTarget];
    startAngle = [target_ rotation];
    if(turn) {
        ((CCNode*)target_).position = ccpAdd(center, ccp(-radius, 0));
    } else {
        ((CCNode*)target_).position = ccpAdd(center, ccp(radius, 0));
    }    
}

-(void) update:(ccTime)t {
    // XXX: shall I add % 360
    float rotate =  (startAngle + 360.0f * t );
    if (turn) {
        rotate *= -1;
    }
    [target_ setRotation:rotate];
    
    float fradian = rotate * M_PI / 180.0f;
    CGPoint pos = ccp(center.x + radius * sinf(fradian),
                      center.y + radius * cosf(fradian));
    [target_ setPosition: pos];
}

-(CCActionInterval*) reverse {    
    BOOL result = !turn;
    return [[self class] actionWithDuration:duration_ turn:result center:center radius:radius];
}

@end

用例(新建一个 CCLayer 子类,把下面这段代码放在 -(id) init 方法里面即可):

-(void) testRotateEx {
    CGSize winSize = [[CCDirector sharedDirector] winSize];
    CGPoint pntAnchor = ccp(winSize.width / 2.0f, 0);
    
    // 锚点精灵~
    CCSprite* spAnchor = [CCSprite spriteWithFile:@"blackCircle.png"];
    [spAnchor setPosition:pntAnchor];
    [self addChild:spAnchor z:0 tag:TAG_SPRITE_ANCHOR];
    
    // 旋转体精灵~
    CCSprite* spTree = [CCSprite spriteWithFile:@"tree.png"];
    
    id rotate = [BYRoundBy actionWithDuration:3.0f turn:YES center:pntAnchor radius:winSize.height/2.0f];
    id action = [CCRepeatForever actionWithAction:rotate];
    [spTree runAction:action];
    
    [self addChild:spTree z:0 tag:TAG_SPRITE_TREE];
}




你可能感兴趣的:(cocos2d ccsprite 偏心转动)