CCCardinalSplineBy 样条曲线动作


欢迎各位童鞋转载,转载请注明出处:http://blog.csdn.net/song_hui_xiang

作者新浪微博:http://weibo.com/u/3168848533

作者腾讯微博:http://t.qq.com/song_huixiang

CCCardinalSplineBy概念

这个类是样条曲线动作,其创建函数CCCardinalSplineBy::create(float duration, cocos2d::CCPointArray *points, float tension);中duration是时间间隔,points是控制点列表,tension是松紧程度。tension==1时,样条线是分段直线。tension<1向外松弛弯曲,tension>1向内缩紧弯曲。By动作是以当前坐标为新坐标原点。

CCCardinalSplineBy示例–为同一组控制点实现不同的移动路径
   
  1. CCSize s = CCDirector::sharedDirector()->getWinSize();
  2.     CCPointArray *array = CCPointArray::create(20);
  3.     array->addControlPoint(ccp(0, 0));
  4.     array->addControlPoint(ccp(s.width/2-30, 0));
  5.     array->addControlPoint(ccp(s.width/2-30, s.height-80));
  6.     array->addControlPoint(ccp(0, s.height-80));
  7.     array->addControlPoint(ccp(0, 0));

  8.         //
  9.         // sprite 1 (By)
  10.         //
  11.         // Spline with no tension (tension==0)
  12.         //

  13.     CCCardinalSplineBy *action = CCCardinalSplineBy::create(3, array, 0);
  14.     CCActionInterval *reverse = action->reverse();

  15.     CCFiniteTimeAction *seq = CCSequence::create(action, reverse, NULL);

  16.     m_tamara->setPosition(ccp(50, 50));
  17.     m_tamara->runAction(seq);

  18.         //
  19.         // sprite 2 (By)
  20.         //
  21.         // Spline with high tension (tension==1)
  22.         //

  23.     CCCardinalSplineBy *action2 = CCCardinalSplineBy::create(3, array, 1);
  24.     CCActionInterval *reverse2 = action2->reverse();

  25.     CCFiniteTimeAction *seq2 = CCSequence::create(action2, reverse2, NULL);

  26.     m_kathia->setPosition(ccp(s.width/2, 50));
  27.     m_kathia->runAction(seq2);
  28. 由于By动作是以当前坐标为新坐标原点,所以在draw函数里设置openGL的原点位置
  29.     // move to 50,50 since the "by" path will start at 50,50
  30.         kmGLPushMatrix();
  31.         kmGLTranslatef(50, 50, 0);
  32.         ccDrawCardinalSpline(m_pArray, 0, 100);
  33.         kmGLPopMatrix();

  34.         CCSize s = CCDirector::sharedDirector()->getWinSize();

  35.         kmGLPushMatrix();
  36.         kmGLTranslatef(s.width/2, 50, 0);
  37.         ccDrawCardinalSpline(m_pArray, 1, 100);
  38.         kmGLPopMatrix();
复制代码

效果图如下:


你可能感兴趣的:(CCCardinalSplineBy 样条曲线动作)