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

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

    luogu P1965 转圈游戏 题目 #include<iostream> #include<cstdlib> #include<cstdio> #include ...

  2. Yii中CreateUrl的使用总结

    在Yii中经常要生成URL,不管是为了自动跳转还是仅仅是一个链接.下面对Yii中的URL生成做了一个总结.提示:以下controllerX代表控制器X,actionX代表方法X.在Controller ...

  3. python学习第五天--函数进阶

    局部变量与全局变量下面代码中,old_price,rite为全局变量,final_price为局部变量 globals() 声明全局变量,在函数内可修改函数外的变量 内嵌函数:函数当中嵌套函数 闭包: ...

  4. vmware 15安装centos 7.6

    环境: Vmware 15 centos 7.6.1810(CentOS-7-x86_64-DVD-1810.iso) centos:centos是linux发行版之一,开源,免费,安装的iso可以向 ...

  5. P3773 [CTSC2017]吉夫特

    传送门 看到组合数在模 $2$ 意义下的乘积,考虑用 $lucas$ 定理把组合数拆开 $lucas$ 告诉我们,$C(n,m)$ 在模 $k$ 意义下的值,相当于 $n,m$ 在 $k$ 进制下每一 ...

  6. 攻防世界--logmein

    测试文件:https://adworld.xctf.org.cn/media/task/attachments/a00849bb514c413f8a6526f6bb56c628 1.准备 得到信息 6 ...

  7. (转)yum的$releasever真是太反动了

    Posted on 2009年 10月9日 by JulyClyde 来看这篇文章的人,大都应该同意<Unix编程艺术>中提到的那些观点吧.今天就给大家看一个反例:yum 的 $relea ...

  8. usb server新产品(旧老板设备)-给自己一个学习硬件的动力

  9. Vue 侦听属性

    Vue 提供了一种更通用的方式来观察和响应 Vue 实例上的数据变动:侦听属性 <!DOCTYPE html> <html> <head> <meta cha ...

  10. flask中间件请求流程

    from flask import Flask,session,url_for,request,flash,get_flashed_messages app = Flask(__name__) app ...