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:
#ifndef __CIRCLE_MOVE_ACT_H__@H_301_66@ |
#define __CIRCLE_MOVE_ACT_H__@H_301_66@
#include "cocos2d.h"@H_301_66@
//http://202.113.29.3/nankaisource/mathhands/Elementary%20mathematics/0103/010301/01030101.htm@H_301_66@
{@H_301_66@
@H_301_66@bool@H_301_66@initWithDuration(@H_301_66@float@H_301_66@duration,@H_301_66@const@H_301_66@cocos2d::CCPoint& center,@H_301_66@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_301_66@
@H_301_66@
static@H_301_66@
CircleMoveAct* create(@H_301_66@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_301_66@:@H_301_66@
m_duration;@H_301_66@
m_scaleDiff;@H_301_66@
m_currScale;@H_301_66@
m_angle;@H_301_66@
m_anglePreFrame;@H_301_66@
cocos2d::CCPoint m_initPos;@H_301_66@
};@H_301_66@
#endif // __CIRCLE_MOVE_ACT_H__@H_301_66@
CircleMoveAct.cpp:
#include "CircleMoveAct.h"@H_301_66@
USING_NS_CC;@H_301_66@
CircleMoveAct* CircleMoveAct::create(@H_301_66@
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_301_66@
pRet->initWithDuration(duration,center,scale,angle);@H_301_66@
pRet->autorelease();@H_301_66@
}@H_301_66@
{@H_301_66@
->m_center = center;@H_301_66@
->m_scaleDiff = scaleDiff;@H_301_66@
->m_currScale = 1.0f;@H_301_66@
->m_angle = angle;@H_301_66@
->m_anglePreFrame = angle / duration * CCDirector::sharedDirector()->getAnimationInterval() / (180 / M_PI);@H_301_66@
->m_frameCnts = 0;@H_301_66@
CCZone* pNewZone = NULL;@H_301_66@
CircleMoveAct* pCopy = NULL;@H_301_66@
pCopy = (CircleMoveAct*)(pZone->m_pCopyObject);@H_301_66@
}@H_301_66@
else@H_301_66@
CCActionInterval::copyWithZone(pZone);@H_301_66@
pCopy->initWithDuration(m_duration,m_center,m_scaleDiff,m_angle);@H_301_66@
CC_SAFE_DELETE(pNewZone);@H_301_66@
pCopy;@H_301_66@
CircleMoveAct::startWithTarget(CCNode *pTarget)@H_301_66@
CCActionInterval::startWithTarget(pTarget);@H_301_66@
m_initPos = pTarget->getPosition();@H_301_66@
m_frameCnts++;@H_301_66@
m_currScale += m_scaleDiff;@H_301_66@
CCPoint newPos = ccpRotateByAngle(m_initPos,m_frameCnts * m_anglePreFrame);@H_301_66@
CCPoint diff = ccpSub(newPos,m_center);@H_301_66@
newPos = diff * m_currScale + m_center;@H_301_66@
@H_301_66@
m_pTarget->setPosition(newPos);@H_301_66@
//debug@H_301_66@
#if 0@H_301_66@
CCDrawNode *node = CCDrawNode::create();@H_301_66@
node->drawDot(newPos,3,ccc4f(128,128,128));@H_301_66@
m_pTarget->getParent()->addChild(node);@H_301_66@
#endif@H_301_66@
}@H_301_66@