http://blog.csdn.net/hereiskxm/article/details/14910243?reload 根据这个修改的
头文件:
class RotateWithAction:public cocos2d::CCActionInterval
{
public:
CCObject* copyWithZone(CCZone* pZone);
RotateWithAction(void);
~RotateWithAction(void);
static RotateWithAction* create(CCActionInterval* action,float offRotate=0);
virtual void startWithTarget(CCNode *pTarget);
bool initWithAction(CCActionInterval* pAction);
void setOffRotation(float offRotate);
void stop();
void update(float dt);
protected:
void SetInnerAction(CCActionInterval* pAction);
CCNode* pInnerTarget;
CCActionInterval* pInnerAction;
float m_offRotation;
};
实现代码:
RotateWithAction::RotateWithAction()
:pInnerAction(NULL)
,pInnerTarget(NULL)
{
}
RotateWithAction::~RotateWithAction(void)
{
CC_SAFE_RELEASE(pInnerAction);
}
RotateWithAction* RotateWithAction::create( CCActionInterval* pAction,float offrotate)
{
RotateWithAction* action = new RotateWithAction();
if(action && action->initWithAction(pAction)){
action->autorelease();
action->setOffRotation(offrotate);
return action;
}
CC_SAFE_DELETE(action);
return NULL;
}
void RotateWithAction::setOffRotation(float offRotate)
{
m_offRotation = offRotate;
}
bool RotateWithAction::initWithAction( CCActionInterval* pAction )
{
if(!CCActionInterval::initWithDuration(pAction->getDuration()))
return false;
pAction->retain();
pInnerAction = pAction;
return true;
}
void RotateWithAction::startWithTarget( CCNode *pTarget )
{
pInnerTarget=pTarget;
CCAction::startWithTarget(pTarget);
pInnerAction->startWithTarget(pTarget);
}
void RotateWithAction::stop()
{
pInnerAction->stop();
CCActionInterval::stop();
}
void RotateWithAction::update(float dt)
{
CCPoint prePos=pInnerTarget->getPosition();
pInnerAction->update(dt);
CCPoint curPos=pInnerTarget->getPosition();
float degree = (curPos-prePos).getAngle();//求本帧位置与前帧位置相连形成的夹角。
degree = CC_RADIANS_TO_DEGREES(-degree);//坐标的方向跟rotation的方向是相反的,需要*-1
// float degree=atan2((curPos.y-prePos.y),(curPos.x-prePos.x));
// degree=-degree/3.141592f*180;//根据弧度求出角度
pInnerTarget->setRotation(degree+m_offRotation); //设置图片旋转角度
}
void RotateWithAction::SetInnerAction( CCActionInterval* pAction )
{
if (pInnerAction!=pAction)
{
CC_SAFE_RELEASE(pInnerAction);
pInnerAction=pAction;
CC_SAFE_RETAIN(pInnerAction);
}
}
CCObject* RotateWithAction::copyWithZone( CCZone* pZone )
{
CCZone* pNewZone=NULL;
RotateWithAction* pCopy=NULL;
if(pZone&&pZone->m_pCopyObject)
{
pCopy=(RotateWithAction*)(pZone->m_pCopyObject);
}
else
{
pCopy=new RotateWithAction();
pZone=pNewZone=new CCZone(pCopy);
}
CCActionInterval::copyWithZone(pZone);
pCopy->initWithAction(dynamic_cast<CCActionInterval*>(pInnerAction->copy()->autorelease()));
CC_SAFE_DELETE(pNewZone);
return pCopy;
}