惯例先上图。
环境:WIN32 + cocos2d-x2.1.4。
m_pPoints = CCPointArray::create(5); m_pPoints->retain(); // 这里的坐标均为closeNormal为原点进行计算,即以closeNormal为参照点 m_pPoints->addControlPoint(ccp(0, 0)); m_pPoints->addControlPoint(ccp(s.width / 2 - 50, 0)); m_pPoints->addControlPoint(ccp(s.width / 2 - 50, s.height - 100)); m_pPoints->addControlPoint(ccp(0, s.height - 100)); m_pPoints->addControlPoint(ccp(0, 0)); CCSprite* closeNormal = CCSprite::create("CloseNormal.png"); closeNormal->setPosition(ccp(s.width / 2, 50)); this->addChild(closeNormal, 1); CCCardinalSplineBy* actionBy = CCCardinalSplineBy::create(3.0F, m_pPoints, 1); closeNormal->runAction(CCSequence::create(actionBy, actionBy->reverse(), NULL));
void HelloWorld::draw() { kmGLPushMatrix(); // 设置颜色 ccDrawColor4B(255, 0, 0, 255); // 设置矩形框坐标点 kmGLTranslatef(240, 50, 0); ccDrawCardinalSpline(m_pPoints, 1, 100); kmGLPopMatrix(); }
总结:
CCCardinalSplineBy::create(float duration, cocos2d::CCPointArray *points, float tension);中duration是时间间隔,points是控制点列表,tension是松紧程度。tension==1时,样条线是分段直线。tension<1向外松弛弯曲,tension>1向内缩紧弯曲。By动作是以当前坐标为新坐标原点。