cocos2dx 自定义抛物线action

前端之家收集整理的这篇文章主要介绍了cocos2dx 自定义抛物线action前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
cocos2dx的动作里面有很多action,包括位移,贝赛尔曲线等,但是没有抛物线,做游戏时我们有时候需要让物体以抛物线来运动,贝塞尔曲线虽然可以模拟抛物线,但是用户使用两次后就会发现每次的抛物线都不一样,我们可以通过自定义action来实现抛物线。原理就是加速度和位置的关系

首先,我们新建一个ParabolaMoveAct.h和ParabolaMoveAct.cpp。

下面是.h文件代码

#ifndef __PARABOLAMOVEACT_H__ #define __PARABOLAMOVEACT_H__ #include "cocos2d.h" USING_NS_CC; class ParabolaMoveAct : public cocos2d::CCActionInterval { public: bool initWithDuration(const CCPoint& m_now,float m_gx,float m_gy,float m_vx,float m_vy); virtual cocos2d::CCObject* copyWithZone(cocos2d::CCZone* pZone); virtual void startWithTarget(cocos2d::CCNode *pTarget); virtual void update(float time); public: static ParabolaMoveAct* create(const CCPoint& m_now,float m_vy); protected: cocos2d::CCPoint m_now; float m_gx; float m_gy; float m_vx; float m_vy; }; #endif // __HELLOWORLD_SCENE_H__ 然后是.cpp的代码 #include "ParabolaMoveAct.h" USING_NS_CC; ParabolaMoveAct* ParabolaMoveAct::create(const CCPoint& m_now,float m_vy) { ParabolaMoveAct *pRet = new ParabolaMoveAct(); pRet->initWithDuration(m_now,m_gx,m_gy,m_vx,m_vy); pRet->autorelease(); return pRet; } bool ParabolaMoveAct::initWithDuration(const CCPoint& m_now,float m_vy) { if (CCActionInterval::initWithDuration(100)) { this->m_now = m_now; this->m_gx=m_gx; this->m_gy=m_gy; this->m_vx=m_vx; this->m_vy=m_vy; return true; } return false; } CCObject* ParabolaMoveAct::copyWithZone(CCZone *pZone) { CCZone* pNewZone = NULL; ParabolaMoveAct* pCopy = NULL; if(pZone && pZone->m_pCopyObject) { pCopy = (ParabolaMoveAct*)(pZone->m_pCopyObject); } else { pCopy = new ParabolaMoveAct(); pZone = pNewZone = new CCZone(pCopy); } CCActionInterval::copyWithZone(pZone); pCopy->initWithDuration(m_now,m_vy); CC_SAFE_DELETE(pNewZone); return pCopy; } void ParabolaMoveAct::startWithTarget(CCNode *pTarget) { CCActionInterval::startWithTarget(pTarget); m_now = pTarget->getPosition(); } void ParabolaMoveAct::update(float time) { /* m_frameCnts++; m_currScale += m_scaleDiff; CCPoint newPos = ccpRotateByAngle(m_initPos,m_center,m_frameCnts * m_anglePreFrame); CCPoint diff = ccpSub(newPos,m_center); newPos = diff * m_currScale + m_center; m_pTarget->setPosition(newPos);*/ this->m_vx+=this->m_gx; this->m_vy+=this->m_gy; m_now.x+=this->m_vx; m_now.y+=this->m_vy; m_pTarget->setPosition(m_now); } 调用方式和cocos2dx里面的其他action相同 原文链接:https://www.f2er.com/cocos2dx/346511.html

猜你在找的Cocos2d-x相关文章