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

  每一个游戏程序都有一个循环在不断运行,它是由导演对象来管理与维护。如果需要场景中的精灵运动起来,可以在游戏循环中使用定时器对精灵等对象进行操作。因为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类常用函数方法

 void scheduler(
const ccSchedulerFun& callback, //回调方法
void* target, //目标执行者
float interval, //间隔时间,如果为0每帧都执行
unsigned int repeat, //重复执行动作
float delay, //延迟执行
bool paused, //如果为true暂停执行
const std::string& Key //键值标识该定时器
);
//取消key所对应的定时器
void unschedule(const std::string& key, void* target)
//取消selector定时器
void unschedule(SEL_SCHEDULE selector, Ref* target)
//取消更新update方法
void unscheduleUpdate(void* target);
//取消所有target定时器(可能有多个图层)
void unscheduleAllForTarget(void* target);
//取消所有定时器
void unscheduleAll(void);
//暂停定时器
void pauseTarget(void* target);
//恢复定时器
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();
}

Cocos2d-x之定时器的更多相关文章

  1. cocos2d JS 利用定时器实现-倒计时功能

    //创建一个定时器 cc.director.getScheduler().schedule(this, this.updates, 1, cc.REPEAT_FOREVER, 0, false, &q ...

  2. 6.cocos2d设置定时器

    T1LayerAnchorPoint.h #pragma once #include "cocos2d.h" USING_NS_CC; class T1LayerAnchorPoi ...

  3. cocos2d 定时器

    //获取当前系统的语言 LanguageType language=CCApplication::sharedApplication()->getCurrentLanguage(); //每一帧 ...

  4. Cocos2d-x 3.2 学习笔记(十六)保卫萝卜 游戏主循环与定时器

    保卫萝卜~想法一直存在于想法,实战才是硬道理!有想法就去实现,眼高手低都是空谈.   一.游戏主循环GameSchedule      主循环是游戏处理逻辑,控制游戏进度的地方,处理好主循环是很重要的 ...

  5. cocos2dx 3.x(定时器或延时动作自动调用button的点击响应事件)实现自动内测

    // // ATTGamePoker.hpp // MalaGame // // Created by work on 2016/11/09. // // #ifndef ATTGamePoker_h ...

  6. cocos2dx 3.x(一张背景图利用定时器实现循环轮播)

    // // MainScene.hpp // helloworld // // Created by apple on 16/9/19. // // #ifndef MainScene_hpp #de ...

  7. 【转】 实现 Cocos2d-x 全局定时器

    转自:http://www.tairan.com/archives/3998 cocos2d-x 中有自己的定时器实现,一般用法是在场景,层等内部实现,定时器的生命周期随着它们的消亡而消亡,就运行周期 ...

  8. cocos2d 高仿doodle jump 无源代码

    1. 游戏视频 主角眼熟吗?没错,上次跑酷游戏中的"30"来Jump了,有三种道具.主角光环,竹蜻蜓.翅膀: 有两种怪物,螃蟹和鸟: 有5种板子.点击屏幕,30会把它的嘴巴3给发射 ...

  9. cocos2d 游戏开发实战

    文章转自:http://uliweb.clkg.org/tutorial/read/40 6   cocos2d 游戏开发实战 6.1   创建cocos2d项目 6.2   cocos2d v3 & ...

  10. Cocos2d-x 源代码分析 : Scheduler(定时器) 源代码分析

    源代码版本号 3.1r,转载请注明 我也最终不out了,開始看3.x的源代码了.此时此刻的心情仅仅能是wtf! !!!!!!! !.只是也最终告别CC时代了. cocos2d-x 源代码分析文件夹 h ...

随机推荐

  1. DevExpress Winform使用单例运行程序方法和非DevExpress使用Mutex实现程序单实例运行且运行则激活窗体的方法

    原文:DevExpress Winform使用单例运行程序方法和非DevExpress使用Mutex实现程序单实例运行且运行则激活窗体的方法 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA ...

  2. 并行开发 2.task

    原文:8天玩转并行开发——第二天 Task的使用 在我们了解Task之前,如果我们要使用多核的功能可能就会自己来开线程,然而这种线程模型在.net 4.0之后被一种称为基于 “任务的编程模型”所冲击, ...

  3. JVM(14)之 类加载机制

    开发十年,就只剩下这套架构体系了! >>>   从本篇博文开始,我们就进入虚拟机类加载机制的学习了.那么什么是类加载呢?当我们写完一个Java类的时候,并不是直接就可以运行的,它还要 ...

  4. spring(五):spring中Aware接口的使用

    spring中自定义组件需要使用spring的底层组件时,可以通过自定义组件实现相关XxxAware接口,重写其中的方法进而实现 例如:自定义一个组件,该组件中需要使用ApplicationConte ...

  5. 【问题解决方案】Mathtype中丢失Mplugin.dll的问题

    网络上搜索到的答案: Mathtype中丢失Mplugin.dll,把Mplugin.dll文件放到Mathtype安装根目录下就好了. 然而试过以后仍然不行 事实是: 如果下载的mathtype安装 ...

  6. javascript:变量声明&&赋值的提升和函数声明&&定义的提升在不同情况下的表现

    console.log(a); //undefined console.log(show); //函数的定义 show();         //aaa123 var a = 1; function ...

  7. ES6——函数-箭头函数

    箭头函数: 1.普通函数 function 函数名(){...} 2.箭头函数 注意:  1)如果只有一个返回值,{}return可以省略: let arr = [12,5,8,99,33,14,26 ...

  8. 有关css的兼容问题

    兼容性 1    页面在不同浏览器中可能显示不同  在IE6下 子级的宽度会撑开父级设置好的宽度   温馨提示:和模型的计算一定要精确,IE浏览器可能显示不同   兼容性 2    在IE6中,元素浮 ...

  9. #import,#include与@class的区别

    1.#include是C中用来引用文件的关键字,而#import是obj-c中用来代替include的关键字.#import可以确保同一个文件只能被导入一次,从而避免了使用#include容易引起的重 ...

  10. vue,一路走来(9)--聊天窗口

    闲暇时间,介绍一下我做一个聊天窗口的心得.如图: 首先要考虑的是得判断出是自己的信息还是对方发来的信息,给出如图的布局,切换不同的类. <li class="clearfix" ...