Cocos2d-x 随运动方向转变图片方向实现方式 (改版)

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值