cocos2dx学习笔记:自定义动作实现圆周运动

前端之家收集整理的这篇文章主要介绍了cocos2dx学习笔记:自定义动作实现圆周运动前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

From: http://cstriker1407.info/blog/cocos2dx-study-notes-custom-actions-realization-of-circular-motion/

最近在翻帖子的时候发现很多大牛都自己实现自定义动作,而不是通过各种动作进行组合,正好最近需要一个圆周运动的效果,就自己写了一个自定义的动作,这里备注下大致的实现思路。

备注:

该动作并未实际应用在游戏中,可能有bug。

数学复习:

截图来自【http://202.113.29.3/nankaisource/mathhands/Elementary%20mathematics/0103/010301/01030101.htm

实现方式:

可参考这篇文章来实现:【http://www.jb51.cc/article/p-fdrufoeg-bcr.html

作者自己也写了一个功能增强版的圆周运动,可以实现螺旋线式的圆周运动。代码比较简单,就不在细说了。

自己的实现:

CircleMoveAct.h:

@H_502_70@
#define __CIRCLE_MOVE_ACT_H__ @H_502_70@
@H_502_70@
#include "cocos2d.h" @H_502_70@
@H_502_70@
classCircleMoveAct :publiccocos2d::CCActionInterval @H_502_70@
{ @H_502_70@
public: @H_502_70@
boolinitWithDuration(floatduration,constcocos2d::CCPoint& center,scaleDiff,255)!important; border:0px!important; white-space:normal; margin:0px!important; outline:0px!important; text-align:left!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; font-weight:normal!important; font-style:normal!important; min-height:inherit!important">angle); @H_502_70@
@H_502_70@
virtualcocos2d::CCObject* copyWithZone(cocos2d::CCZone* pZone); @H_502_70@
virtualvoidstartWithTarget(cocos2d::CCNode *pTarget); @H_502_70@
update(floattime); @H_502_70@
staticCircleMoveAct* create(scale,61)!important; border:0px!important; white-space:normal; margin:0px!important; outline:0px!important; text-align:left!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; font-weight:bold!important; font-style:normal!important; min-height:inherit!important">protected: @H_502_70@
m_duration; @H_502_70@
cocos2d::CCPoint m_center; @H_502_70@
m_scaleDiff; @H_502_70@
m_currScale; @H_502_70@
m_angle; @H_502_70@
m_anglePreFrame; @H_502_70@
intm_frameCnts; @H_502_70@
cocos2d::CCPoint m_initPos; @H_502_70@
}; @H_502_70@
#endif // __CIRCLE_MOVE_ACT_H__ @H_502_70@

CircleMoveAct.cpp:

#include "CircleMoveAct.h" @H_502_70@
USING_NS_CC; @H_502_70@
CircleMoveAct* CircleMoveAct::create(CCPoint& center,255)!important; border:0px!important; white-space:normal; margin:0px!important; outline:0px!important; text-align:left!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; font-weight:normal!important; font-style:normal!important; min-height:inherit!important">angle) @H_502_70@
CircleMoveAct *pRet =newCircleMoveAct(); @H_502_70@
pRet->initWithDuration(duration,center,scale,angle); @H_502_70@
pRet->autorelease(); @H_502_70@
returnpRet; @H_502_70@
} @H_502_70@
CircleMoveAct::initWithDuration(angle) @H_502_70@
{ @H_502_70@
if(CCActionInterval::initWithDuration(duration)) @H_502_70@
this->m_duration = duration; @H_502_70@
->m_center = center; @H_502_70@
->m_scaleDiff = scaleDiff; @H_502_70@
->m_currScale = 1.0f; @H_502_70@
->m_angle = angle; @H_502_70@
/************************************************************************/ @H_502_70@
/* 计算每次update调用时需要转动的弧度 */ @H_502_70@
->m_anglePreFrame = angle / duration * CCDirector::sharedDirector()->getAnimationInterval() / (180 / M_PI); @H_502_70@
->m_frameCnts = 0; @H_502_70@
returntrue; @H_502_70@
falseCCObject* CircleMoveAct::copyWithZone(CCZone *pZone) @H_502_70@
CCZone* pNewZone = NULL; @H_502_70@
CircleMoveAct* pCopy = NULL; @H_502_70@
if(pZone && pZone->m_pCopyObject) @H_502_70@
pCopy = (CircleMoveAct*)(pZone->m_pCopyObject); @H_502_70@
} @H_502_70@
else @H_502_70@
pCopy =CircleMoveAct(); @H_502_70@
pZone = pNewZone =CCZone(pCopy); @H_502_70@
CCActionInterval::copyWithZone(pZone); @H_502_70@
pCopy->initWithDuration(m_duration,m_center,m_scaleDiff,m_angle); @H_502_70@
CC_SAFE_DELETE(pNewZone); @H_502_70@
pCopy; @H_502_70@
CircleMoveAct::startWithTarget(CCNode *pTarget) @H_502_70@
CCActionInterval::startWithTarget(pTarget); @H_502_70@
m_initPos = pTarget->getPosition(); @H_502_70@
CircleMoveAct::update() @H_502_70@
m_frameCnts++; @H_502_70@
m_currScale += m_scaleDiff; @H_502_70@
CCPoint newPos = ccpRotateByAngle(m_initPos,m_frameCnts * m_anglePreFrame); @H_502_70@
CCPoint diff = ccpSub(newPos,m_center); @H_502_70@
newPos = diff * m_currScale + m_center; @H_502_70@
@H_502_70@
m_pTarget->setPosition(newPos); @H_502_70@
//debug @H_502_70@
#if 0 @H_502_70@
CCDrawNode *node = CCDrawNode::create(); @H_502_70@
node->drawDot(newPos,3,ccc4f(128,128,128)); @H_502_70@
m_pTarget->getParent()->addChild(node); @H_502_70@
#endif @H_502_70@
} @H_502_70@

正常调用方式:

CCSize size = CCDirector::sharedDirector()->getVisibleSize(); @H_502_70@
CCSprite *test = CCSprite::create("CloseNormal.png"test->setPosition(ccp(size.width/2 + 20,size.height/2));//设置起点 @H_502_70@
test->setAnchorPoint(ccp(0.5,0.5)); @H_502_70@
//设置用时,圆心,是否缩放(不缩放设置为0.0f),旋转角度 @H_502_70@
CircleMoveAct *act = CircleMoveAct::create(10,ccp(size.width/2,size.height/2),0.0f,1500); @H_502_70@
test->runAction(act); @H_502_70@
->addChild(test); @H_502_70@

截图如下:

缩放调用方式:

//设置用时,圆心,缩放递增值0.01f,旋转角度 @H_502_70@
->addChild(test); @H_502_70@

截图如下:

猜你在找的Cocos2d-x相关文章

#ifndef __CIRCLE_MOVE_ACT_H__