圆弧动作类

  1. /* 
  2. 圆弧动作类 
  3. */  
  4. class CCArcBy : public cocos2d::CCActionInterval  
  5. {  
  6. public:  
  7.     //初始化圆弧动作类  
  8.     //duration: 动作类的持续时间  
  9.     //ptCenter: 圆弧的中心点  
  10.     //deltaAngle: 弧度的变化量,用正负来表示逆时针或顺时针方向  
  11.     bool initWithDuration(float duration, const cocos2d::CCPoint& ptCenter, float deltaAngle);  
  12.       
  13.     virtual CCObject* copyWithZone(cocos2d::CCZone* pZone);  
  14.     virtual void startWithTarget(cocos2d::CCNode *pTarget);  
  15.     virtual CCActionInterval* reverse(void);  
  16.     virtual void update(float time);  
  17.   
  18. public:  
  19.     static CCArcBy* create(float duration, const cocos2d::CCPoint& ptCenter, float deltaAngle);  
  20.   
  21. protected:  
  22.     cocos2d::CCPoint m_startPosition;  
  23.     cocos2d::CCPoint m_previousPosition;  
  24.   
  25.     cocos2d::CCPoint m_ptCenter;  
  26.     float            m_fAngleDelta;  
  27. };  
/*
圆弧动作类
*/
class CCArcBy : public cocos2d::CCActionInterval
{
public:
	//初始化圆弧动作类
	//duration: 动作类的持续时间
	//ptCenter: 圆弧的中心点
	//deltaAngle: 弧度的变化量,用正负来表示逆时针或顺时针方向
	bool initWithDuration(float duration, const cocos2d::CCPoint& ptCenter, float deltaAngle);
	
	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;
};

实现文件:

  1. CCArcBy* CCArcBy::create(float duration, const CCPoint& ptCenter, float deltaAngle)  
  2. {  
  3.     CCArcBy* pRet= new CCArcBy();  
  4.     pRet->initWithDuration(duration, ptCenter, deltaAngle);  
  5.     pRet->autorelease();  
  6.   
  7.     return pRet;  
  8. }  
  9.   
  10. bool CCArcBy::initWithDuration(float duration, const CCPoint& ptCenter, float deltaAngle)  
  11. {  
  12.     if(CCActionInterval::initWithDuration(duration))  
  13.     {  
  14.         m_ptCenter= ptCenter;  
  15.         m_fAngleDelta= deltaAngle;  
  16.         return true;  
  17.     }  
  18.   
  19.     return false;  
  20. }  
  21.   
  22. CCObject* CCArcBy::copyWithZone(CCZone* pZone)  
  23. {  
  24.     CCZone* pNewZone = NULL;  
  25.     CCArcBy* pCopy = NULL;  
  26.     if(pZone && pZone->m_pCopyObject)   
  27.     {  
  28.         //in case of being called at sub class  
  29.         pCopy = (CCArcBy*)(pZone->m_pCopyObject);  
  30.     }  
  31.     else  
  32.     {  
  33.         pCopy = new CCArcBy();  
  34.         pZone = pNewZone = new CCZone(pCopy);  
  35.     }  
  36.   
  37.     CCActionInterval::copyWithZone(pZone);  
  38.   
  39.     pCopy->initWithDuration(m_fDuration, m_ptCenter, m_fAngleDelta);  
  40.   
  41.     CC_SAFE_DELETE(pNewZone);  
  42.     return pCopy;  
  43. }  
  44.   
  45. void CCArcBy::startWithTarget(CCNode *pTarget)  
  46. {  
  47.     CCActionInterval::startWithTarget(pTarget);  
  48.     m_previousPosition = m_startPosition = pTarget->getPosition();  
  49. }  
  50.   
  51. CCActionInterval* CCArcBy::reverse()  
  52. {  
  53.     return CCArcBy::create(m_fDuration, m_ptCenter, -m_fAngleDelta);  
  54. }  
  55.   
  56. void CCArcBy::update(float time)  
  57. {  
  58.     CCLog("%f", time);  
  59.     if(m_pTarget)  
  60.     {  
  61. #if CC_ENABLE_STACKABLE_ACTIONS  
  62.         CCPoint currentPos = m_pTarget->getPosition();  
  63.         CCPoint diff = ccpSub(currentPos, m_previousPosition);  
  64.         m_startPosition = ccpAdd( m_startPosition, diff);  
  65.         CCPoint newPos =  m_ptCenter + ccpRotateByAngle(m_startPosition-m_ptCenter, CCPointZero, m_fAngleDelta*time);  
  66.         m_pTarget->setPosition(newPos);  
  67.         m_pTarget->setRotation(-CC_RADIANS_TO_DEGREES(m_fAngleDelta*time));  
  68.         m_previousPosition = newPos;  
  69. #else  
  70.         m_pTarget->setPosition(m_ptCenter + ccpRotateByAngle(m_startPosition-m_ptCenter, CCPointZero, m_fAngleDelta*time));  
  71. #endif  // CC_ENABLE_STACKABLE_ACTIONS  
  72.     }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值