博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cocos2d ccsprite 偏心转动
阅读量:4708 次
发布时间:2019-06-10

本文共 3358 字,大约阅读时间需要 11 分钟。

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

原文链接:

让 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 
{ // 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];}

转载于:https://www.cnblogs.com/yang3wei/archive/2012/08/12/2739659.html

你可能感兴趣的文章
做衡八的日子(转自VFleaking)
查看>>
day7.条件和循环
查看>>
(转)log4j(二)——如何控制日志信息的输出?
查看>>
JavaScript简介
查看>>
php.ini中safe_mode开启对PHP系统函数的影响
查看>>
gdb
查看>>
字符串与整数、浮点数、无符号整数之间的转换常用函数
查看>>
ubuntu清理旧内核
查看>>
有关UIImageView+AFNetworking 下载图片的线程问题
查看>>
Node之安装篇
查看>>
Android的Animation之LayoutAnimation使用方法
查看>>
二分图最大匹配算法-Hopcroft-Karp模板
查看>>
发布和订阅的删除
查看>>
如何使用qtp12 utf进行功能测试
查看>>
使用LinQ进行增删改查
查看>>
索引作用 和缺点
查看>>
eclipse 背景颜色
查看>>
Tomcat连接池配置-DBCP
查看>>
aliyun阿里云Maven仓库地址和其他地址
查看>>
Mercedes-Benz won’t start| Step by Step Troubleshooting Guide
查看>>