【深入了解cocos2d-x 3.x】定时器(scheduler)的使用和原理探究(1)

注:本文開始。引擎升级到cocos2dx 3.6

在游戏开发过程中。常常会遇到使用计时器的情况,比如:倒计时,定时炸弹等

scheduler是cocos2dx 2.x时代就已经存在的产物,主要用于各种延时函数以及各种每帧运行的函数。本文主要介绍scheduler的API函数以及用法。

首先,全部继承Node的类都能够使用scheduler,下面是Node类下相关API的介绍

    /**
     * 开启自带的update方法,这种方法会每帧运行一次,默认优先级为0。而且在全部自己定义方法运行之前运行
     */
    void scheduleUpdate(void);

    /**
     * 开启自带的update方法,这种方法会每帧运行一次。设定的优先级越小,越优先运行
     */
    void scheduleUpdateWithPriority(int priority);

    /**
     * 关闭自带的update方法
     */
    void unscheduleUpdate(void);

    /**
     * 定义一个自己定义的定时器
	 * selector:回调函数
	 * interval:反复间隔时间,反复运行间隔的时间。假设传入0,则表示每帧调用
	 * repeat:反复运行次数,假设传入CC_REPEAT_FOREVER则表示无限循环
	 * delay:延时秒数。延迟delay秒開始运行第一次回调
     */
    void schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay);

    /**
	 * 定义一个马上运行的无限循环的自己定义定时器
	 * interval:反复间隔时间,反复运行间隔的时间,假设传入0,则表示每帧调用
     */
    void schedule(SEL_SCHEDULE selector, float interval);

    /**
     * 定义一个延迟运行的单次自己定义定时器
     * delay:延时秒数。延迟delay秒開始运行第一次回调
     */
    void scheduleOnce(SEL_SCHEDULE selector, float delay);

    /**
     * 定义了一个无限循环、马上运行而且每帧都会运行的自己定义定时器(和scheduleUpdate效果一样,可是会在scheduleUpdate之后运行)
     */
    void schedule(SEL_SCHEDULE selector);

	 /**
     * 使用lambda函数定义一个每帧调用的自己定义定时器
     * callback:lambda函数
     * key:lambda函数的Key,用于取消定时器
     */
    void schedule(const std::function<void(float)>& callback, const std::string &key);

    /**
     * 使用lambda函数定义一个每隔interval秒调用的自己定义定时器
     * callback:lambda函数
	 * interval:反复间隔时间。反复运行间隔的时间,假设传入0。则表示每帧调用
     * key:lambda函数的Key,用于取消定时器
     */
    void schedule(const std::function<void(float)>& callback, float interval, const std::string &key);

    /**
     * 使用lambda函数定义一个自己定义定时器
     * callback:lambda函数
	 * interval:反复间隔时间,反复运行间隔的时间。假设传入0,则表示每帧调用
	 * repeat:反复运行次数。假设传入CC_REPEAT_FOREVER则表示无限循环
	 * delay:延时秒数。延迟delay秒開始运行第一次回调
     * key:lambda函数的Key。用于取消定时器
     * @lua NA
     */
    void schedule(const std::function<void(float)>& callback, float interval, unsigned int repeat, float delay, const std::string &key);

	
    /**
     * 取消一个自己定义定时器
     */
    void unschedule(SEL_SCHEDULE selector);

    /**
     * 取消本节点上全部的定时器(包含scheduleUpdate开启的定时器)
     */
    void unscheduleAllSelectors(void);


接下来用一个简单的倒计时样例来学习API的使用。首先看看实现效果:


详细代码例如以下:

bool HelloWorld::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
	//int _time;
	//Label *_countdownLabel; //类成员变量
	_time = 10;
	_countdownLabel = Label::create(String::createWithFormat("%d", _time)->getCString(), "微软雅黑", 24);
	_countdownLabel->setPosition(Vec2(200, 200));
	addChild(_countdownLabel);
	//方法1
	//schedule(schedule_selector(HelloWorld::NextTime),1.f,CC_REPEAT_FOREVER,0.0f);
	//方法2
	schedule([&](float dt)
	{
		if (_time == 0)
		{
			unschedule("CountDown");
			_countdownLabel->setString("end");
		}
		else
		{
			--_time;
			_countdownLabel->setString(String::createWithFormat("%d", _time)->getCString());
		}
	}, 1.f, CC_REPEAT_FOREVER, 0.0f, "CountDown");
	return true;
}

void HelloWorld::NextTime(float dt)
{
	if (_time == 0)
	{
		unschedule(schedule_selector(HelloWorld::NextTime));
		_countdownLabel->setString("end");
	}
	else
	{
		--_time;
		_countdownLabel->setString(String::createWithFormat("%d", _time)->getCString());
	}
}


方法1与方法2实现的效果全然一样,差别在于一个使用了Lambda函数。一个使用了传统的方法。


接下来简单说一下scheduleOnce。这个函数用于延时运行。比方能够实现2秒后删除自身。详细代码例如以下:

	scheduleOnce([this](float dt)
	{
		this->removeFromParent();
	},2.f, "Del");

scheduleOnce是一个非常实用的函数,在非常多场合都会用到。

然后再来看看scheduleUpdate,这是一个与其它函数有所差别。启动的是一个Node::update方法。启动之后这种方法每帧运行。而且会优先于自己定义计时器运行,也能够指定优先级。

接下来我们想实现一个移动动画,每秒钟固定向左边移动一定的距离,通过scheduleUpdate实现,下面是实现代码:

bool HelloWorld::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
	_moveX = 100.0f;
	scheduleUpdate();
}
void HelloWorld::update(float dt)
{
	float moveX = _moveX * dt;
	_countdownLabel->setPositionX(_countdownLabel->getPositionX() + moveX);
}

本文通过一些基础的定时器应用分析了cocos2dx中scheduler的基本用法,在项目中会有很多其它定时器的应用场景。下一篇文章将对创建定时器的实现进行分析。


【推广】 免费学中医,健康全家人
原文地址:https://www.cnblogs.com/llguanli/p/8723679.html