Cocos2d-x之定时器

|   版权声明:本文为博主原创文章,未经博主允许不得转载。

 

  每一个游戏程序都有一个循环在不断运行,它是由导演对象来管理与维护。如果需要场景中的精灵运动起来,可以在游戏循环中使用定时器对精灵等对象进行操作。因为Node类封装了Scheduler类,所以也可以直接使用Node中调用函数。      

定时器的两种实现方式:
>>.

scheduleUpdate();   
    是定时器更新函数,如果定时器更新了,就会调用update();来执行update();函数继承自Node类;每个Node对象只要调用该函数,那么这个Node对象就会定时的每帧回调用一次自己的update(float delat)函数
    void update(float dt);                  
    dt的默认频率是: 1/60    

>>.

schedule(schedule_selector(selector));
void myUpdate(float dt);
schedule_selector();                      这是指定定时器的一个回调方法,这个方法我们可以自己去定义。


实例:

.h files

#ifndef _TIMERTEST_SCENE_H_
#define _TIMERTEST_SCENE_H_
#include "cocos2d.h"
USING_NS_CC;
class timerTest : public cocos2d::Layer
{
private:
public:
	static cocos2d::Scene* createScene();
	virtual bool init();
	virtual void update(float delta);
	void schedule_TwoTips_Test(float delta);

	CREATE_FUNC(timerTest);
};
#endif // _TIMERTEST_SCENE_H_



.cpp files

#include "TimerTest.h"
float timeCount = 0;
Scene* timerTest::createScene()
{
	auto scene = Scene::create();
	auto layer = timerTest::create();
	scene->addChild(layer);
	return scene;
}
bool timerTest::init()
{
	if (!Layer::init())
	{
		return false;
	}
	//启动定时器,调用scheduleUpdate()来执行定时器
	//scheduleUpdate();
	//two tips
	schedule(schedule_selector(timerTest::schedule_TwoTips_Test), 2);
	return true;
}
//delta是刷新的帧频率;在默认的情况下,默认值为: 1/60s刷新一次
void timerTest::update(float delta)
{
	timeCount += delta;
	if (timeCount > 5.0f)
	{
		CCLOG("Pause Update...");
		MessageBox("5s后停止定时器...", "定时器测试");
		unscheduleUpdate();
	}
	CCLOG("Begin Update...");
}
void timerTest::schedule_TwoTips_Test(float delta)
{
	CCLOG("Two tips of Update...");
}

 

  

 

Scheduler类常用函数方法

 1 void scheduler(
 2     const ccSchedulerFun& callback,                      //回调方法
 3     void* target,                                        //目标执行者
 4     float interval,                                      //间隔时间,如果为0每帧都执行                
 5     unsigned int repeat,                                 //重复执行动作
 6     float delay,                                         //延迟执行
 7     bool paused,                                         //如果为true暂停执行
 8     const std::string& Key                               //键值标识该定时器
 9 );
10 //取消key所对应的定时器
11 void unschedule(const std::string& key, void* target)
12 //取消selector定时器
13 void unschedule(SEL_SCHEDULE selector, Ref* target)
14 //取消更新update方法
15 void unscheduleUpdate(void* target);
16 //取消所有target定时器(可能有多个图层)
17 void unscheduleAllForTarget(void* target);
18 //取消所有定时器
19 void unscheduleAll(void);
20 //暂停定时器
21 void pauseTarget(void* target);
22 //恢复定时器
23 void resumeTarget(void* target);

 

实例:

.h files

#ifndef _SCHEDULERTEST_SCENE_H_
#define _SCHEDULERTEST_SCENE_H_
#include "cocos2d.h"
USING_NS_CC;
//scheduler类的常用函数
class schedulerTest : public cocos2d::Layer
{
private:
	cocos2d::Size visible;
public:
	static cocos2d::Scene* createScene();
	virtual bool init();
	virtual void update(float delta);
	void callBack_1(float delta);
	void callBack_2(float delta);
	void start(Ref* sendef);
	void pause(cocos2d::Ref* sendef);
	void resume(cocos2d::Ref* sendef);
	void stop(cocos2d::Ref* sendef);
	CREATE_FUNC(schedulerTest);
};
#endif //_SCHEDULERTEST_SCENE_H_



.cpp files

#include "Scheduler_Test.h"
Scene* schedulerTest::createScene()
{
	Scene* scene = Scene::create();
	schedulerTest* layer = schedulerTest::create();
	scene->addChild(layer);
	return scene;
}
bool schedulerTest::init()
{
	if (!Layer::init())
	{
		return false;
	}
	visible = Director::getInstance()->getWinSize();
	auto item1 = MenuItemFont::create("Start", CC_CALLBACK_1(schedulerTest::start, this));
	auto item2 = MenuItemFont::create("Pause", CC_CALLBACK_1(schedulerTest::pause, this));
	auto item3 = MenuItemFont::create("Resume", CC_CALLBACK_1(schedulerTest::resume, this));
	auto item4 = MenuItemFont::create("Stop", CC_CALLBACK_1(schedulerTest::stop, this));
	auto menu = Menu::create(item1, item2, item3, item4, NULL);
	menu->setPosition(Vec2(visible.width / 2, visible.height / 2));
	menu->alignItemsVerticallyWithPadding(20.0f);
	this->addChild(menu);
	return true;
}
void schedulerTest::update(float delta)
{
	CCLOG("update...");
}
//自己定义定时器回调的方法
void schedulerTest::callBack_1(float delta)
{
	CCLOG("callback_one..");
}
void schedulerTest::callBack_2(float delta)
{
	CCLOG("callback_two...");
}
void schedulerTest::start(Ref* sendef)
{
	scheduleUpdate();
	schedule(schedule_selector(schedulerTest::callBack_1));
	schedule(schedule_selector(schedulerTest::callBack_2));
}
void schedulerTest::pause(cocos2d::Ref* sendef)
{
	//要暂停定时器首先要先得到要暂停的对象
	auto schedulerflag = Director::getInstance()->getScheduler();
	//然后让得到的对象暂停,下面这条语句表示暂停当前索取对象所在的那个层
	schedulerflag->pauseTarget(this);
}
void schedulerTest::resume(cocos2d::Ref* sendef)
{
	//同样要恢复定时器,则要首先指定要恢复哪个对象的定时器,所以我们要先取得,恢复定时器的那个对象
	auto schedulerflag = Director::getInstance()->getScheduler();
	schedulerflag->resumeTarget(this);
}
void schedulerTest::stop(cocos2d::Ref* sendef)
{
	auto schedulerflag = Director::getInstance()->getScheduler();
	schedulerflag->unscheduleAll();
}

posted @ 2016-08-23 13:11  Geore  阅读(489)  评论(0编辑  收藏  举报