cocos2dx[2.x](3)--timer update schedule/update

【nagging】

定时器在大部分游戏中是不可或缺的,即每隔一段时间,就要执行相应的刷新体函数,以更新游戏的画面、时间、进度、敌人的指令等等。

cocos2dx为我们提供了定时器schedule相关的操作。其操作函数的定义在CCNode中,所以基本上大多数的引擎类都可以设置定时器,如CCLayer、CCSprite、CCMenu等。

定时器更新的方式分为三类:

(1)默认定时器  :scheduleUpdate();
(2)自定义定时器:schedule();
(3)一次性定时器:scheduleOnce();

[Demo download]
https://github.com/shahdza/Cocos_LearningTest/tree/master/demo_%E5%AE%9A%E6%97%B6%E5%99%A8schedule%E3%80%81update

【3.x】

几乎无变化。

【scheduleUpdate】

默认定时器:scheduleUpdate()。

该定时器默认刷新次数与屏幕刷新频率有关。如频率为60帧每秒,那么scheduleUpdate每秒执行60次刷新。

与scheduleUpdate其对应的刷新函数体为update(),即每一帧会执行一次update()函数。

相关操作如下:
    //开启默认定时器。刷新间隔为一帧。
    void scheduleUpdate();
    void scheduleUpdateWithPriority(int priority); //给予优先级priority。priority越小,优先级越高

    virtual void update(float delta); //update为scheduleUpdate定时器的刷新函数体.

【schedule】

自定义定时器:schedule()。

该定时器可以自定义指定的刷新函数体、刷新函数体的次数、刷新频率、以及开始刷新的时间。

函数体不一定为update(),可以自己定义。

相关操作如下:
    //设置自定义定时器。默认刷新间隔为一帧。
    //      interval  :   每隔interval秒,执行一次。
    //      repeat    :   重复次数。
    //      delay     :   延迟时间,即创建定时器delay秒后开始执行刷新。
    //schedule( schedule_selector(HelloWorld::myUpdate), 1.0/60.0 );
    void schedule(SEL_SCHEDULE selector); //默认刷新间隔为一帧
    void schedule(SEL_SCHEDULE selector, float interval); //自定义刷新间隔,单位:秒
    void schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay);

【scheduleOnce】

一次性定时器:scheduleOnce()。

该定时器在等待delay秒延迟时间后,只执行一次刷新函数体,之后就不再刷新。

相关操作如下:
    //只执行一次,delay秒后执行
    //scheduleOnce( schedule_selector(HelloWorld::myUpdate), 5.0 ); 
    void scheduleOnce(SEL_SCHEDULE selector, float delay);

【Other operations】

定时器的取消、暂停、恢复。
相关操作如下:
    //this->unscheduleUpdate();
    //sprite->unscheduleAllSelectors();
    void unscheduleUpdate(void);            //取消默认定时器
    void unschedule(SEL_SCHEDULE selector); //取消自定义函数的定时器
    void unscheduleAllSelectors(void);      //取消所有定时器
    void pauseSchedulerAndActions(void);    //暂停所有定时器和动作
    void resumeSchedulerAndActions(void);   //恢复所有定时器和动作

【Code combat】

1. Create five sprites in HelloWorld::init() The
sprites and five methods for defining timers correspond one-to-one.

    //创建五个精灵
        CCSprite* sp = CCSprite::create("Icon.png");
        sp->setPosition( ccp(30, mysize.height - 30) );
        this->addChild(sp, 0, 100); //tag标记100

        CCSprite* sp1 = CCSprite::create("Icon.png");
        sp1->setPosition( ccp(30, mysize.height - 90) );
        this->addChild(sp1, 0, 101); //tag标记101

        CCSprite* sp2 = CCSprite::create("Icon.png");
        sp2->setPosition( ccp(30, mysize.height - 150) );
        this->addChild(sp2, 0, 102); //tag标记102

        CCSprite* sp3 = CCSprite::create("Icon.png");
        sp3->setPosition( ccp(30, mysize.height - 210) );
        this->addChild(sp3, 0, 103); //tag标记103

        CCSprite* sp4 = CCSprite::create("Icon.png");
        sp4->setPosition( ccp(30, mysize.height - 270) );
        this->addChild(sp4, 0, 104); //tag标记104


    //定义五个定时器,更新精灵
        this->scheduleUpdate();
        this->schedule( schedule_selector(HelloWorld::myupdate) );
        this->schedule( schedule_selector(HelloWorld::myupdate2), 1.0f );
        this->schedule( schedule_selector(HelloWorld::myupdate3), 1.0f, 5, 3.0f);
        this->scheduleOnce( schedule_selector(HelloWorld::myupdate4), 5.0f );

2. Write the refresh function body corresponding to the timer

 //scheduleUpdate
    void HelloWorld::update(float dt)
    {
        CCSprite* sp = (CCSprite*)this->getChildByTag(100); //获取 tag=100 的精灵
        sp->setPosition( sp->getPosition() + ccp(1,0) );    //每帧移动1
    }

    //schedule(schedule_selector)
    void HelloWorld::myupdate(float dt)
    {
        CCSprite* sp1 = (CCSprite*)this->getChildByTag(101); //获取 tag=101 的精灵
        sp1->setPosition( sp1->getPosition() + ccp(1,0) );   //每帧移动1
    }

    //schedule(schedule_selector, interval)
    void HelloWorld::myupdate2(float dt)
    {
        CCSprite* sp2 = (CCSprite*)this->getChildByTag(102); //获取 tag=102 的精灵
        sp2->setPosition( sp2->getPosition() + ccp(60,0) );  //每秒移动60
    }

    //schedule(schedule_selector, interval, repeat, delay)
    void HelloWorld::myupdate3(float dt)
    {
        CCSprite* sp3 = (CCSprite*)this->getChildByTag(103); //获取 tag=103 的精灵
        sp3->setPosition( sp3->getPosition() + ccp(60,0) );  //每秒移动60
    }

    //scheduleOnce
    void HelloWorld::myupdate4(float dt)
    {
        CCSprite* sp4 = (CCSprite*)this->getChildByTag(104); //获取 tag=104 的精灵
        sp4->setPosition( sp4->getPosition() + ccp(100,0) ); //移动100
    }

3. Running results
write picture description here

4. Analysis and conclusion

(1)scheduleUpdate()和schedule(schedule_selector)的效果一样,只是schedule可以自定义刷新函数体,不一定是update()。而scheduleUpdate()的刷新函数体只能为update()。

(2)schedule(schedule_selector, interval):设置了interval=1.0,所以每隔1.0秒执行了一次myupdate2()。

(3)schedule(schedule_selector, interval, repeat, delay):在开始3.0f秒后才开始执行myupdate3(),并且之后又重复执行了5次,就停止更新了。

(4)scheduleOnce(schedule_selector):在开始5秒后,只执行了一次myupdate4(),就停止更新了。
转载网址:http://blog.51cto.com/shahdza/1542014

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326932738&siteId=291194637