加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2d-x实现node圆弧运动 (附源代码)

发布时间:2020-12-14 19:13:03 所属栏目:百科 来源:网络整理
导读:纪录下自己写的东西! 头文件: /*圆弧动作类*/class CCArcBy : public cocos2d::CCActionInterval{public://初始化圆弧动作类//duration: 动作类的持续时间//ptCenter: 圆弧的中心点//deltaAngle: 弧度的变化量,用正负来表示逆时针或顺时针方向bool initWi


纪录下自己写的东西!

头文件:

/*
圆弧动作类
*/
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,float deltaAngle);

protected:
	cocos2d::CCPoint m_startPosition;
	cocos2d::CCPoint m_previousPosition;

	cocos2d::CCPoint m_ptCenter;
	float            m_fAngleDelta;
};

实现文件:
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,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 class
		pCopy = (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_fAngleDelta);
}

void CCArcBy::update(float time)
{
	CCLog("%f",time);
	if(m_pTarget)
	{
#if CC_ENABLE_STACKABLE_ACTIONS
		CCPoint 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;
#else
		m_pTarget->setPosition(m_ptCenter + ccpRotateByAngle(m_startPosition-m_ptCenter,m_fAngleDelta*time));
#endif  // CC_ENABLE_STACKABLE_ACTIONS
	}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读