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

  每一个游戏程序都有一个循环在不断运行,它是由导演对象来管理与维护。如果需要场景中的精灵运动起来,可以在游戏循环中使用定时器对精灵等对象进行操作。因为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. 对于nginx配置文件中的fastcgi_param相关参数的理解

    今天在ubuntu中搭建LNMP的时候,遇到了一个问题 在浏览器中访问.php文件的时候,nginx不能正常解析,页面只是空白,后来百度了一下,发现了问题 在nginx的配置文件nginx.conf中 ...

  2. luoguP2123 皇后游戏(贪心)

    luoguP2123 皇后游戏(贪心) 题目 洛谷题目chuanso 题解 有一篇好题解,我就懒得推式子了,毕竟打到电脑上还是很难的 牛逼题解传送门 code #include<iostream ...

  3. Python_pickle

    pickle是一个可以将任意一个对象存储在硬盘文件中的工具. 更新:Python3中用法变了:  https://www.cnblogs.com/fmgao-technology/p/9078918. ...

  4. JVM(13)之 阶段回顾

    开发十年,就只剩下这套架构体系了! >>>   各位小伙伴,到上一篇博文为止,我们的内存模型相关知识就已经讲完了!讲!完!了!不知道大家吸收了多少,这里我们简单的来回顾一下吧!    ...

  5. WEEX-EROS开发小笔记

    本文是作者之前刚接触移动端跨平台开发,使用weex-eros开发项目平日里记下来的一些笔记,分享出来方便为新手解惑,weex-eros是weex的一套解决方法,使用vue语法糖,对于前端开发者来说可以 ...

  6. quotacheck - 扫描文件系统,创建,检测并修补配额文件

    总览(SYNOPSIS) quotacheck [ -agucfinvdFR ] filesystem 描述(DESCRIPTION) quotacheck 察看每一个文件系统,建立当前磁盘使用情况表 ...

  7. go语言从例子开始之Example33.工作池

    在这个例子中,我们将看到如何使用 Go 协程和通道实现一个工作池 . Example: package main import "fmt" import "time&qu ...

  8. linux ---apache的安装和配置

    linux环境下的安装:yum安装和tar包安装 yum安装: 首先安装php环境 yum install php55w yum install php55w-mysql yum install ph ...

  9. 【LeetCode】几何学 geometry(共2题)

    [587]Erect the Fence [892]Surface Area of 3D Shapes

  10. vue之click事件绑定

    1.@click不光可以绑定方法,也可以就地修改data里的数据 代码示例代码如下: 2.@click绑定多个操作的时候用:隔开 代码示例代码如下: <el-table><el-ta ...