From: http://cstriker1407.info/blog/cocos2dx-study-notes-custom-actions-realization-of-circular-motion/
最近在翻帖子的时候发现很多大牛都自己实现自定义动作,而不是通过各种动作进行组合,正好最近需要一个圆周运动的效果,就自己写了一个自定义的动作,这里备注下大致的实现思路。
Contents[hide]
1备注: 2数学复习: 3实现方式: 备注:该动作并未实际应用在游戏中,可能有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__ |
//http://202.113.29.3/nankaisource/mathhands/Elementary%20mathematics/0103/010301/01030101.htm
class
CircleMoveAct :
public
cocos2d::CCActionInterval
{
public:
bool
initWithDuration(
float
duration,
const
cocos2d::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);
virtual
cocos2d::CCObject* copyWithZone(cocos2d::CCZone* pZone);
virtualvoID
startWithTarget(cocos2d::CCNode *pTarget);
update(float
time
);
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
:
m_duration; cocos2d::CCPoint m_center;
m_scaleDiff; m_currScale; m_angle; m_anglePreFrame; intm_frameCnts;
cocos2d::CCPoint m_initPos; }; #endif // __CIRCLE_MOVE_ACT_H__ CircleMoveAct.cpp:
#include "CircleMoveAct.h" USING_NS_CC; 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)
CircleMoveAct *pRet =new
CircleMoveAct();
pRet->initWithDuration(duration,center,scale,angle); pRet->autorelease(); returnpRet;
} CircleMoveAct::initWithDuration(
angle)
{ if(CCActionInterval::initWithDuration(duration))
this->m_duration = duration;
->m_center = center; ->m_scaleDiff = scaleDiff; ->m_currScale = 1.0f; ->m_angle = angle; /************************************************************************/
/* 计算每次update调用时需要转动的弧度 */ ->m_anglePreFrame = angle / duration * CCDirector::sharedDirector()->getAnimationInterval() / (180 / M_PI); ->m_frameCnts = 0; returntrue
;
falseCCObject* CircleMoveAct::copyWithZone(CCZone *pZone)
CCZone* pNewZone = NulL; CircleMoveAct* pcopy = NulL; if(pZone && pZone->m_pcopyObject)
pcopy = (CircleMoveAct*)(pZone->m_pcopyObject); } else pcopy =CircleMoveAct();
pZone = pNewZone =CCZone(pcopy);
CCActionInterval::copyWithZone(pZone); pcopy->initWithDuration(m_duration,m_center,m_scaleDiff,m_angle); CC_SAFE_DELETE(pNewZone); pcopy; CircleMoveAct::startWithTarget(CCNode *pTarget) CCActionInterval::startWithTarget(pTarget); m_initPos = pTarget->getposition(); CircleMoveAct::update()
m_frameCnts++; m_currScale += m_scaleDiff; CCPoint newPos = ccpRotateByAngle(m_initPos,m_frameCnts * m_anglePreFrame); CCPoint diff = ccpsub(newPos,m_center); newPos = diff * m_currScale + m_center; m_pTarget->setposition(newPos); //deBUG #if 0 CCDrawNode *node = CCDrawNode::create(); node->drawDot(newPos,3,ccc4f(128,128,128)); m_pTarget->getParent()->addChild(node); #endif } 正常调用方式:
CCSize size = CCDirector::sharedDirector()->getVisibleSize(); CCSprite *test = CCSprite::create("Closenormal.png"
test->setposition(ccp(size.wIDth/2 + 20,size.height/2));
//设置起点
test->setAnchorPoint(ccp(0.5,0.5)); //设置用时,圆心,是否缩放(不缩放设置为0.0f),旋转角度 CircleMoveAct *act = CircleMoveAct::create(10,ccp(size.wIDth/2,size.height/2),0.0f,1500); test->runAction(act); ->addChild(test); 截图如下:
缩放调用方式:
//设置用时,圆心,缩放递增值0.01f,旋转角度 ->addChild(test);截图如下:
总结
以上是内存溢出为你收集整理的cocos2dx学习笔记:自定义动作实现圆周运动全部内容,希望文章能够帮你解决cocos2dx学习笔记:自定义动作实现圆周运动所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)