cocos2d-x基本知识点(6)---动作系列(中)

来源:互联网 发布:如何更改淘宝登录名字 编辑:程序博客网 时间:2024/05/11 19:00

基本样条动作

画基本样条路径:CCCardinalSplineTo和CCCardinalSplineBy的用法

void ActionCardinalSpline::onEnter(){

ActionsDemo::onEnter();

this->centerSprites(2);

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));

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);

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);

m_pArray = array;

array->ratain();

}

//CCCardinalSplineBy定义数组时,第一个点最好设置为(0,0),否则起点会被忽略掉,可以重写布景层的draw函数来把路径画出来

CCCatmullRomTo和CCCatmullRomBy的用法:

void ActionCatmullRom::onEnter(){

ActionsDemo::onEnter();

this->centerSprites(2);

CCSize s = CCDirector::sharedDirector()->getWInSize();

m_tamara->setPosition(ccp(50,50));

CCPointArray* array = CCPointArray::create(20);

array->addControlPoint(ccp(0,0));

array->addControlPoint(ccp(80,80));

array->addControlPoint(ccp(s.width-80,80));

array->addControlPoint(ccp(s.width-80,s.height-80));

array->addControlPoint(ccp(80,s.height-80));

array->addControlPoint(ccp(80,80));

array->addControlPoint(ccp(s.width/2,s.height/2));

CCCatmullRomBy* action = CCCatmullRomBy::create(3,array);

CCFiniteTimeAction* reverse = action->reverse();

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

m_tamara->runAction(seq);

CCPointArray* array2 = CCPointArray::create(20);

array2->addControlPoint(ccp(s.width/2,30));

array2->addControlPoint(ccp(s.width-80,30));

array2->addControlPoint(ccp(s.widht-80,s.height-80));

array2->addControlPoint(ccp(s.width/2,s.height-80));

array2->addControlPoint(ccp(s.width/2,30));

CCCatmullRomTo* action2 = CCCatmullRomTo::create(3,array2);

CCFiniteTimeAction* reverse2 = action2->reverse();

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

m_kathia->runAction(seq2);

m_pArray1 = array;

m_pArray1->retain();

m_pArray2 = array2;

m_pArray2->retain();

}

缓冲动作

实现一些加速度或者减速度的效果。Ease系列的方法改变了运动的速度,但是并没有改变总体时间,如果整个动作持

续5s,那个整个时间仍然会持续5s。




加上基本的缓冲动作,一共6种缓冲动作。

缓冲动作的使用:

//基本缓冲动作

CCEaseIn::create((CCActionInterval*)(move->copy()->autorelease()),2.5f); //速率

CCEaseOut::create(CCActionInterval*)(move->copy()->autorelease()),2.5f);

CCEaseInOut::create((CCActionInterval*)(move->copy()->autorelease()),2.5f);

//指数缓冲动作

CCEaseExponentialIn::create((CCActionInterval*)(move->copy()->autorelease()));

CCEaseExponentialOut::create((CCActionInterval*)(move->copy()->autorelease()));

//赛因缓冲动作

CCEaseSineIn::create((CCActionInterval*)(move->copy()->autorelease()));

CCEaseSineOut::create((CCActionInterval*)(move->copy()->autorelease()));

CCEaseSineInOut::create((CCActionInterval*)(move->copy()->autorelease()));

//跳跃缓冲动作

CCEaseBounceIn::create((CCActionInterval*)(move->copy()->release()));

CCEaseBounceOut::create((CCActionInterval*)(move->copy()->release()));

CCEaseBounceInOut::create((CCActionInterval*)(move->copy()->release()));

//弹性缓冲动作

CCEaseElasticIn::create((CCActionInterval*)(move->copy()->autorelease()));

CCEaseElasticOut::create((CCActionInterval*)(move->copy()->autorelease()));

CCEaseElasticInOut::create((CCActionInterval*)(move->copy()->autorelease()),0.3f);//振动周期,默认0.3

//回震缓冲动作

CCEaseBackIn::create((CCActionInterval*)(move->copy()->autorelease()));

CCEaseBackOut::create((CCActionInterval*)(move->copy()->autorelease()));

CCEaseBackInOut::create((CCActionInterval*)(copy->copy()->release()));

组合动作 

定义一个动作序列,可以使用动作的CCArray数组;也可以把所有的动作作为参数参数传入create函数中,

最后结尾参数使用NULL即可;还可以把两个有限时间动作按顺序传入create函数中。

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

m_kathia->runAction(seq2);

CCSpawn动作时使被合成的动作同时进行:

CCAction* action = CCSpawn::create(CCJumpBy::create(2,CCPointMake(300,0),50,4),

CCRotateBy::create(2,720),NULL);

CCRepeat 和 CCRepeatForever:

CCActionInterval* rep2= CCRepeat::create((CCFiniteTimeAction*)(seq->copy()->autorelease()),10)

m_kathia->runAction(rep2);

CCAction* rep2= CCRepeatForever::create((CCActionInterval*)(seq->copy()->autorelease()));

m_kathia->runAction(rep2);

跟随动作CCFollow

void ActionFollow::onEnter(){

ActionDemo::onEnter();

centerSprites(1);

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

m_gross->setPostion(CCPointMake(-200,s.height/2));

CCActionInterval* move = CCMoveBy::create(2,CCPointMake(s.width*3,0));

CCActionInterval* move_back = move->reverse();

CCFiniteTimeAction* seq = CCSequence::create(move,move_back,NULL);

CCAction *rep = CCRepeatForever::create((CCActionInterval*)seq);

m_grossini->runAction(rep);

this->runAction(CCFollow::create(m_grossini,CCRectMake(0,0,s.width*2-100,s.height)));

}

可调整速度动作CCSpeed

void SpeedTest::onEnter(){

EaseSpriteDemo::onEnter();

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

CCActionInterval* jump1 = CCJumpBy::create(4,CCPointMake(-s.width+80,0),100,4);

CCActionInterval* jump2 = jump1->reverse();

CCActionInterval* rot1 = CCRotateBy::create(4,360*2);

CCActionInterval* rot2 = rot1->reverse();

CCFiniteTimeAction* seq3_1 = CCSequence::create(jump2,jump1,NULL);

CCFiniteTimeAction* seq3_2 = CCSequence::create(rot1,rot2,NULL);

CCFiniteTimeAction* spawn= CCSpawn::create(seq3_1,seq3_2,NULL);

CCSpeed* action = CCSpeed::create(CCRepeatForever::create(CCActionInterval*)spawn),1.0f);

action->setTag(kTagAction1);

CCAction* action2 = (CCAction*)(action->copy()->autorelease());

CCAction* action3 = (CCAction*)(action->copy()->autorelease());

action2->setTag(kTagAction1);

action3->setTag(kTagAction1);

m_grossini->runAction(action2);

m_tamara->runAction(action3);

m_kathia->runAction(action);

this->schedule(schedule_selector(SpeedTest::altertime),1.0f);

}

void SpeedTest::altertime(float dt){

CCSpeed* action1 = (CCSpeed*)(m_grossini->getActionByTag(kTagAction1));

CCSpeed* action2 = (CCSpeed*)(m_tamara->getActionByTag(kTagAction1));

CCSpeed* action3 = (CCSpeed*)(m_kathia->getActionByTag(kTagAction1));

action1->setSpeed(CCRANDOM_0_1()*2);

action2->setSpeed(CCRANDOM_0_1()*2);

action3->setSpeed(CCRANDOM_0_1()*2);

}

}





0 0
原创粉丝点击