关于CCRANDOM_0_1
CCRANDOM_0_1的范围是[0,1)包括0但不包括1
CCRANDOM_0_1() * 1400.0f / 100.0f是0-13
另外每次随机都是相同的数,要随机下种子
srand((unsigned int)time(NULL)); for (int i = 0; i < 100; i++) { //CCRANDOM_0_1是[0,1) //log("%d", (int)(CCRANDOM_0_1()*2)); // int idx = (int)(CCRANDOM_0_1() * 1400.0f / 100.0f); log("%d",idx); }
递归一个动画,实现动画不停的播放,动画间隔时间为随机数:
callFunc callFuncN表示里面添加一个节点作为参数,传递到调用方法,均为sequence的回调函数,意思为做完这个动作,再执行这个函数。
void MainScene::aciton_dishu(Node* dishu) { //dishu = Sprite::createWithSpriteFrameName("mouse1.png"); Vector<SpriteFrame*> vec(6); char str2[100] = { 0 }; for (int i = 0; i < 6; i++) { sprintf(str2, "mouse%d.png", (i + 1)); auto frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(str2); vec.pushBack(frame); } float deayT = CCRANDOM_0_1() * 10; log("tag,dely-->%d,%f", dishu->getTag(),deayT); auto animation = Animation::createWithSpriteFrames(vec, 0.2f); //callFunc callFunN callFunNC分别为0,1,2个参数 auto action = Animate::create(animation); Sequence *seq = Sequence::create( DelayTime::create(deayT), action, action->reverse(), //DelayTime::create(deayT), CallFunc::create(std::bind(&MainScene::aciton_dishu, this, dishu)), nullptr); dishu->runAction(seq); }
3.2中callFunc可以通过std::bind绑定任意个数参数到函数里。this是指函数对象指针,后面的是函数参数列表,个人觉得还是CallFunc好用点,CallFuncN不好理解
void ActionRepeatForever::onEnter() { ActionsDemo::onEnter(); centerSprites(1); auto action = Sequence::create( DelayTime::create(1), CallFunc::create( std::bind( &ActionRepeatForever::repeatForever, this, _grossini) ), nullptr); _grossini->runAction(action); } void ActionRepeatForever::repeatForever(Node* sender) { auto repeat = RepeatForever::create( RotateBy::create(1.0f, 360) ); sender->runAction(repeat); }