CCCardinalSplineBy概念
这个类是样条曲线动作,其创建函数CCCardinalSplineBy::create(float duration, cocos2d::CCPointArray *points, float tension);中duration是时间间隔,points是控制点列表,tension是松紧程度。tension==1时,样条线是分段直线。tension<1向外松弛弯曲,tension>1向内缩紧弯曲。By动作是以当前坐标为新坐标原点。
CCCardinalSplineBy示例–为同一组控制点实现不同的移动路径
CCSize s = CCDirector::sharedDirector()->getWinSize(); CCPointArray *array = CCPointArray::create(20); array->addControlPoint(ccp(0, 0)); array->addControlPoint(ccp(s.width/2-30, 0)); array->addControlPoint(ccp(s.width/2-30, s.height-80)); array->addControlPoint(ccp(0, s.height-80)); array->addControlPoint(ccp(0, 0)); // // sprite 1 (By) // // Spline with no tension (tension==0) // CCCardinalSplineBy *action = CCCardinalSplineBy::create(3, array, 0); CCActionInterval *reverse = action->reverse(); CCFiniteTimeAction *seq = CCSequence::create(action, reverse, NULL); m_tamara->setPosition(ccp(50, 50)); m_tamara->runAction(seq); // // sprite 2 (By) // // Spline with high tension (tension==1) // CCCardinalSplineBy *action2 = CCCardinalSplineBy::create(3, array, 1); CCActionInterval *reverse2 = action2->reverse(); CCFiniteTimeAction *seq2 = CCSequence::create(action2, reverse2, NULL); m_kathia->setPosition(ccp(s.width/2, 50)); m_kathia->runAction(seq2);
由于By动作是以当前坐标为新坐标原点,所以在draw函数里设置openGL的原点位置
// move to 50,50 since the "by" path will start at 50,50 kmGLPushMatrix(); kmGLTranslatef(50, 50, 0); ccDrawCardinalSpline(m_pArray, 0, 100); kmGLPopMatrix(); CCSize s = CCDirector::sharedDirector()->getWinSize(); kmGLPushMatrix(); kmGLTranslatef(s.width/2, 50, 0); ccDrawCardinalSpline(m_pArray, 1, 100); kmGLPopMatrix();
效果图如下:
注:
CCPointArray解释
CCPointArray是一个包含控制点的数组,使用方法如上
本文链接:
http://xiandanboke.com.cn/?p=908转载请注明转自
咸蛋博客