Cocos2d-x 之调度器 Scheduler

三种调度器

Cocos中有三种调度器,默认调度器、自定义调度器和单次调度器。

默认调度器 scheduleUpdate

游戏每一帧会调用update方法一次,使用 unScheduleUpdate来移除调度器。

自定义调度器 schedule

可手动指定刷新的频率,使用unSchedule来移除调度器。

我们来看下 schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay) 函数里面的参数:

  • 第一个参数 selector 即为你要添加的事件函数

  • 第二个参数 interval 为事件触发时间间隔

  • 第三个参数 repeat 为触发一次事件后还会触发的次数,默认值为kRepeatForever,表示无限触发次数

  • 第四个参数 delay 表示第一次触发之前的延时

再看schedule的另一种调用方式 schedule(const ccSchedulerFunc &callback,float interval,unsigned int repeat,float delay,const std::string& key)

  • 第一个参数为要添加的回调函数

  • 最后一个参数是给调度器指定一个key,后面unSchedule的时候就可以直接传入一个key即可。

单次调度器scheduleOnce

调度器只执行一次,用 unschedule(SEL_SCHEDULE selector, float delay) 来取消该触发器。

实例

#include "HelloScheduler.h"  

USING_NS_CC;  

Scene* HelloScheduler::createScene() {  
    auto scene = Scene::create();  
    auto layer = HelloScheduler::create();  
    scene->addChild(layer);  
    return scene;  
}  

bool HelloScheduler::init() {  
    if (!Layer::init()) {  
        return false;  
    }  
    auto visibleSize = Director::getInstance()->getVisibleSize();  

    defaultLabel = Label::createWithTTF("default:", "fonts/arial.ttf", 30);  
    customLabel = Label::createWithTTF("custom:", "fonts/arial.ttf", 30);  
    onceLabel = Label::createWithTTF("once:", "fonts/arial.ttf", 30);  

    defaultLabel->setPosition(Vec2(visibleSize.width / 2, visibleSize.height *0.8));  
    customLabel->setPosition(Vec2(visibleSize.width / 2, visibleSize.height *0.6));  
    onceLabel->setPosition(Vec2(visibleSize.width / 2, visibleSize.height *0.4));  

    defaultLabel->setAnchorPoint(Vec2(.5, .5));  
    customLabel->setAnchorPoint(Vec2(.5, .5));  
    onceLabel->setAnchorPoint(Vec2(.5, .5));  

    this->addChild(defaultLabel);  
    this->addChild(customLabel);  
    this->addChild(onceLabel);  

    //默认调度器  
    this->scheduleUpdate();  

    //自定义调度器  
    //this->schedule(schedule_selector(HelloScheduler::updateForCustom), 0.1, kRepeatForever, 0);  
    this->schedule([this](float delta) {  
        customCount++;  
        if (customCount >= 100) {  
            this->unschedule("custom");  
        }  
        std::string str = StringUtils::format("custom:%d   %f", customCount, delta);  
        customLabel->setString(str);  
    }, 0.1, kRepeatForever, 0, "custom");  

    //单次调度器  
    this->scheduleOnce(schedule_selector(HelloScheduler::updateForOnce), 0);  

    return true;  
}  

void HelloScheduler::update(float delta) {  
    defaultCount++;  
    if (defaultCount >= 100) {  
        this->unscheduleUpdate();  
    }  
    std::string str = StringUtils::format("default:%d   %f", defaultCount, delta);  
    defaultLabel->setString(str);  
}  

void HelloScheduler::updateForCustom(float delta) {  
    customCount++;  
    if (customCount >= 100) {  
        this->unschedule(schedule_selector(HelloScheduler::updateForCustom));  
    }  
    std::string str = StringUtils::format("custom:%d   %f", customCount, delta);  
    customLabel->setString(str);  
}  

void HelloScheduler::updateForOnce(float delta) {  
    onceCount++;  
    std::string str = StringUtils::format("once:%8d   %f", onceCount, delta);  
    onceLabel->setString(str);  
    this->unschedule(schedule_selector(HelloScheduler::updateForOnce));  
}  

运行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值