Cocos2d-x to achieve node arc motion (with source code)

Source: Internet
Author: User


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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.