cocos2d-x实例学习(9)之CCCardinalSplineBy

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转载请注明转自 咸蛋博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值