zoukankan      html  css  js  c++  java
  • 关于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);
    }
    

      

  • 相关阅读:
    RTT startup.c 代码学习
    [RTT例程练习] 1.2 静态线程除初始化与脱离
    linux 下生成核心文件
    [RTT例程练习] 2.3 信号量检测按键(同步) (信号量互斥)
    [RTT例程练习] 1.6 线程优先级反转原理
    [RTT例程练习] 1.7 优先级翻转之优先级继承
    extern int Image$$RW_IRAM1$$ZI$$Limit
    [RTT例程练习] 1.1 动态线程创建,删除
    [RTT例程练习] 1.3 线程让出
    Linux操作系统下三种配置环境变量的方法
  • 原文地址:https://www.cnblogs.com/as3lib/p/3917849.html
Copyright © 2011-2022 走看看