• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

chchpd

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

cocos2dx --- Action综合应用三:曲线Action

在学习上两篇博文的基础上,做了一个可以曲线Action。当然基于此可以结合傅里叶变换和欧拉变换做出更炫的效果

思路:基本三角函数变换

实现:

  1.CurveMove头文件

 1 class CC_DLL CCCurveMove : public CCActionInterval
 2 {
 3 public:
 4     /** initializes the action */
 5     bool initWithDuration(float duration, const CCPoint& position,float scaleX,float period);
 6     virtual CCObject* copyWithZone(CCZone* pZone);
 7     virtual void startWithTarget(CCNode *pTarget);
 8     virtual void update(float time);
 9 public:
10     /** creates the action
11     @deprecated: This interface will be deprecated sooner or later.
12     */
13     CC_DEPRECATED_ATTRIBUTE static CCCurveMove* actionWithDuration(float duration, const CCPoint& position,float scaleX,float period);
14     /** creates the action */
15     static CCCurveMove* create(float duration, const CCPoint& position,float scaleY,float period);
16 protected:
17     CCPoint m_endPosition;
18     CCPoint m_startPosition;
19     CCPoint m_delta;
20     float     m_ScaleX;//系数
21     float   m_Period;//周期
22 };

  2. 实现

 1 bool CCCurveMove::initWithDuration(float duration,const CCPoint& pos,float scaleX,float period){
 2     if(CCActionInterval::initWithDuration(duration)){
 3         m_ScaleX = scaleX;
 4         m_Period = period;
 5         m_endPosition = pos;
 6         return true;
 7     }
 8     return false;
 9 }
10 CCObject* CCCurveMove::copyWithZone(CCZone* pZone){
11     CCZone* pNewZone = NULL;
12     CCCurveMove* pCopy = NULL;
13     if(pZone&&pZone->m_pCopyObject){
14         pCopy = (CCCurveMove*)(pZone->m_pCopyObject);
15     }else{
16         pCopy = new CCCurveMove();
17         pZone = pNewZone = new CCZone(pCopy);
18     }
19     CCActionInterval::copyWithZone(pZone);
20     pCopy->initWithDuration(m_fDuration,m_endPosition,m_ScaleX,m_Period);
21     CC_SAFE_DELETE(pNewZone);
22     return pCopy;
23 }
24 void CCCurveMove::startWithTarget(CCNode* pTarget){
25     CCActionInterval::startWithTarget(pTarget);
26     m_startPosition = pTarget->getPosition();
27     m_delta = ccpSub(m_endPosition,m_startPosition);
28 }
29 CCCurveMove* CCCurveMove::actionWithDuration(float duration, const CCPoint& position,float scaleX,float period){
30     return CCCurveMove::create(duration,position,scaleX,period);
31 }
32 CCCurveMove* CCCurveMove::create(float duration, const CCPoint& position,float scaleX,float period){
33     CCCurveMove* pCurveMove = new CCCurveMove();
34     pCurveMove->initWithDuration(duration,position,scaleX,period);
35     pCurveMove->autorelease();
36     return pCurveMove;
37 }
38 void CCCurveMove::update(float time){
39     if(m_pTarget){
40         float yvalue = m_startPosition.y + m_delta.y*time;
41         float xvalue = m_ScaleX*sin(m_Period*yvalue)+200;//the value 200 is not good, should combine with the target device view size
42         m_pTarget->setPosition(xvalue,yvalue);
43     }
44 }

 

posted on 2013-05-09 15:39  chchpd  阅读(560)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3