Record what you write!
Header file:
/* ARC tracking class */class ccarcby: Public cocos2d: ccactioninterval {public: // initialize the arc tracking class // Duration: duration of the sequence class // ptcenter: the center of the arc // deltaangle: the variation of the radians. Positive and Negative values are used to indicate bool initwithduration (float duration, const cocos2d: ccpoint & ptcenter, float deltaangle) in a counterclockwise or clockwise direction ); virtual ccobject * copywithzone (cocos2d: cczone * pzone); Virtual void startwithtarget (cocos2d: ccnode * pTARGET); Virtual ccactioninterval * reverse (void ); virtual void Update (float time); Public: static ccarcby * Create (float duration, const cocos2d: ccpoint & ptcenter, float deltaangle); protected: cocos2d: ccpoint m_startposition; cocos2d:: ccpoint m_previousposition; cocos2d: ccpoint m_ptcenter; float m_fangledelta ;};
Implementation file:
CCArcBy* CCArcBy::create(float duration, const CCPoint& ptCenter, float deltaAngle){CCArcBy* pRet= new CCArcBy();pRet->initWithDuration(duration, ptCenter, deltaAngle);pRet->autorelease();return pRet;}bool CCArcBy::initWithDuration(float duration, const CCPoint& ptCenter, float deltaAngle){if(CCActionInterval::initWithDuration(duration)){m_ptCenter= ptCenter;m_fAngleDelta= deltaAngle;return true;}return false;}CCObject* CCArcBy::copyWithZone(CCZone* pZone){CCZone* pNewZone = NULL;CCArcBy* pCopy = NULL;if(pZone && pZone->m_pCopyObject) {//in case of being called at sub classpCopy = (CCArcBy*)(pZone->m_pCopyObject);}else{pCopy = new CCArcBy();pZone = pNewZone = new CCZone(pCopy);}CCActionInterval::copyWithZone(pZone);pCopy->initWithDuration(m_fDuration, m_ptCenter, m_fAngleDelta);CC_SAFE_DELETE(pNewZone);return pCopy;}void CCArcBy::startWithTarget(CCNode *pTarget){CCActionInterval::startWithTarget(pTarget);m_previousPosition = m_startPosition = pTarget->getPosition();}CCActionInterval* CCArcBy::reverse(){return CCArcBy::create(m_fDuration, m_ptCenter, -m_fAngleDelta);}void CCArcBy::update(float time){CCLog("%f", time);if(m_pTarget){#if CC_ENABLE_STACKABLE_ACTIONSCCPoint currentPos = m_pTarget->getPosition();CCPoint diff = ccpSub(currentPos, m_previousPosition);m_startPosition = ccpAdd( m_startPosition, diff);CCPoint newPos = m_ptCenter + ccpRotateByAngle(m_startPosition-m_ptCenter, CCPointZero, m_fAngleDelta*time);m_pTarget->setPosition(newPos);m_pTarget->setRotation(-CC_RADIANS_TO_DEGREES(m_fAngleDelta*time));m_previousPosition = newPos;#elsem_pTarget->setPosition(m_ptCenter + ccpRotateByAngle(m_startPosition-m_ptCenter, CCPointZero, m_fAngleDelta*time));#endif // CC_ENABLE_STACKABLE_ACTIONS}}
Cocos2d-x to achieve node arc motion (with source code)