cocos2d-x之CCCardinalSplineBy

作者: IOS开发者
发布时间:2015-07-01 17:01:54

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://www.cnblogs.com/chenhaosuibi/p/3411338.html

推荐: