cocos2dx --- Action综合应用三:曲线Action
在学习上两篇博文的基础上,做了一个可以曲线Action。当然基于此可以结合傅里叶变换和欧拉变换做出更炫的效果
思路:基本三角函数变换
实现:
1.CurveMove头文件
1 class CC_DLL CCCurveMove : public CCActionInterval 2 { 3 public: 4 /** initializes the action */ 5 bool initWithDuration(float duration, const CCPoint& position,float scaleX,float period); 6 virtual CCObject* copyWithZone(CCZone* pZone); 7 virtual void startWithTarget(CCNode *pTarget); 8 virtual void update(float time); 9 public: 10 /** creates the action 11 @deprecated: This interface will be deprecated sooner or later. 12 */ 13 CC_DEPRECATED_ATTRIBUTE static CCCurveMove* actionWithDuration(float duration, const CCPoint& position,float scaleX,float period); 14 /** creates the action */ 15 static CCCurveMove* create(float duration, const CCPoint& position,float scaleY,float period); 16 protected: 17 CCPoint m_endPosition; 18 CCPoint m_startPosition; 19 CCPoint m_delta; 20 float m_ScaleX;//系数 21 float m_Period;//周期 22 };
2. 实现
1 bool CCCurveMove::initWithDuration(float duration,const CCPoint& pos,float scaleX,float period){ 2 if(CCActionInterval::initWithDuration(duration)){ 3 m_ScaleX = scaleX; 4 m_Period = period; 5 m_endPosition = pos; 6 return true; 7 } 8 return false; 9 } 10 CCObject* CCCurveMove::copyWithZone(CCZone* pZone){ 11 CCZone* pNewZone = NULL; 12 CCCurveMove* pCopy = NULL; 13 if(pZone&&pZone->m_pCopyObject){ 14 pCopy = (CCCurveMove*)(pZone->m_pCopyObject); 15 }else{ 16 pCopy = new CCCurveMove(); 17 pZone = pNewZone = new CCZone(pCopy); 18 } 19 CCActionInterval::copyWithZone(pZone); 20 pCopy->initWithDuration(m_fDuration,m_endPosition,m_ScaleX,m_Period); 21 CC_SAFE_DELETE(pNewZone); 22 return pCopy; 23 } 24 void CCCurveMove::startWithTarget(CCNode* pTarget){ 25 CCActionInterval::startWithTarget(pTarget); 26 m_startPosition = pTarget->getPosition(); 27 m_delta = ccpSub(m_endPosition,m_startPosition); 28 } 29 CCCurveMove* CCCurveMove::actionWithDuration(float duration, const CCPoint& position,float scaleX,float period){ 30 return CCCurveMove::create(duration,position,scaleX,period); 31 } 32 CCCurveMove* CCCurveMove::create(float duration, const CCPoint& position,float scaleX,float period){ 33 CCCurveMove* pCurveMove = new CCCurveMove(); 34 pCurveMove->initWithDuration(duration,position,scaleX,period); 35 pCurveMove->autorelease(); 36 return pCurveMove; 37 } 38 void CCCurveMove::update(float time){ 39 if(m_pTarget){ 40 float yvalue = m_startPosition.y + m_delta.y*time; 41 float xvalue = m_ScaleX*sin(m_Period*yvalue)+200;//the value 200 is not good, should combine with the target device view size 42 m_pTarget->setPosition(xvalue,yvalue); 43 } 44 }