cocos2d ccsprite 偏心转动
来源:互联网 发布:曲面拟合软件 编辑:程序博客网 时间:2024/05/19 06:35
做了点儿格式上的小改动,写了一个小用例,逻辑还是原作的逻辑~
原文链接: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// 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 偏心转动
- cocos2d - CCSprite
- 偏心
- cocos2d CCSpriteBatchNode 与 CCSprite.
- cocos2d中的ccsprite使用
- Cocos2d-x-->CCSprite 动画
- cocos2d-x ccsprite 详解
- cocos2d-x 中的 CCSprite
- Cocos2D中CCSprite注意事项
- Cocos2d中的CCSprite
- Cocos2d中的CCSprite
- Cocos2d-X中的CCSprite
- cocos2D(四)---- CCSprite
- cocos2D CCSprite 4
- cocos2d-x如何反白高亮CCSprite
- Cocos2d CCSprite 旋转 坐标 ccDrawline
- cocos2d-x学习:CCSprite总结
- cocos2d-X CCsprite点击问题
- Android Launcher开发(六)通过分析Laucher源码整理添加快捷方式的思路
- mysql merge 临时表
- shell 按行读取文件,涉及读配置文件
- 码农+码农=码农 ?
- 优化Pro/E 2001录制快捷键的脚本命令
- cocos2d ccsprite 偏心转动
- Boost.Regex手里有七种武器
- 【VS2005 收纳袋】自动生成Property
- 802.1Q VLAN技术原理
- 阿拉伯数据转中文大写
- vs2010 方便使用git的办法2-免输入密码设置
- javascript:void(0);
- SUSE ORACLE11G EM乱码问题解决
- [线段树] poj 2750 Potted Flower