五毛的cocos2d-x学习笔记07-计时器、数据读写、文件读写
调度器:
定时任务是通过调度器实现的。cocos2d-x推荐用调度器而不是其他方法实现定时任务。Node类都知道如何调度和取消调度事件。
有3种调度器:
- 默认调度器:schedulerUpdate()
- 自定义调度器:schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay)
- schedule(SEL_SCHEDULE selector, float delay)
- 单次调度器:scheduleOnce(SEL_SCHEDULE selector, float delay)
取消调度器:
- 默认调度器:unschedulerUpdate()
- 自定义调度器:unschedule(SEL_SCHEDULE selector, float delay)
- 单次调度器:unschedule(SEL_SCHEDULE selector, float delay)
使用默认调度器,该调度器是使用Node的刷新事件update方法,该方法在每帧绘制之前都会被调用一次。Node类默认没有启用update事件的,所以你需要重写这个update方法。update有一个float类型的形参。
使用自定义调度器:自定义的方法必须要有一个float类型的形参。
使用单次调度器:自定义的方法必须要有一个float类型的形参。
栗子:使用调度器实现定时调用移动文本标签:
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" class HelloWorld : public cocos2d::Layer
{
private:
cocos2d::LabelTTF *label;
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene(); // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init(); // a selector callback
void menuCloseCallback(cocos2d::Ref* pSender); // implement the "static create()" method manually
CREATE_FUNC(HelloWorld); void update(float delta) override; void timerHandler(float delta);
}; #endif // __HELLOWORLD_SCENE_H__
HelloWorldScene.h
#include "HelloWorldScene.h" USING_NS_CC; Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create(); // 'layer' is an autorelease object
auto layer = HelloWorld::create(); // add layer as a child to scene
scene->addChild(layer); // return the scene
return scene;
} // on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
} label = LabelTTF::create("Hello, Cocos", "Courier", ); addChild(label); //scheduleUpdate(); schedule(schedule_selector(HelloWorld::timerHandler), 0.2f); return true;
} void HelloWorld::menuCloseCallback(Ref* pSender)
{
Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit();
#endif
} void HelloWorld::update(float delta){
//log("update");
label->setPosition(label->getPosition()+Vec2(,));
//the following line has the same effect with the above line.
//label->runAction(MoveBy::create(1.0f, Point(1, 1)));
} void HelloWorld::timerHandler(float delta){
label->setPosition(label->getPosition() + Vec2(, ));
}
HelloWorldScene.cpp
首选项:
使用文件存取数据不方便用户的存取。本地数据存储有两种方法,一是UserDefault,二是SQLite数据库。
UserDefault是一个单例类,数据存到以UserDefault命名的xml文件,保存方式是一个map,即key-value的键值对。存取数据通过tinyxml2。
首选项的读取,我觉得类似于Android的SharedPreferences。
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
UserDefault::getInstance()->setStringForKey("data" , "success");
log("%s", UserDefault::getInstance()->getStringForKey("data", "fail").c_str());
return true;
}
init
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>array</key>
<array>
<integer>0</integer>
<integer>1</integer>
<integer>2</integer>
</array>
<key>bool</key>
<true/>
<key>data</key>
<data>
</data>
<key>date</key>
<date>2015-02-16T16:47:11Z</date>
<key>dict</key>
<dict>
<key>age</key>
<string>20</string>
<key>name</key>
<string>Alice</string>
</dict>
<key>number</key>
<integer>123456</integer>
<key>string</key>
<string>hello world!</string>
</dict>
</plist>
test.plist
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
} FileUtils *fu = FileUtils::getInstance();
ValueMap plist = fu->getValueMapFromFile("test.plist"); log("string = %s", (plist["string"].asString()).c_str());
ValueMap& dict = plist["dict"].asValueMap();
log("name = %s", (dict["name"].asString()).c_str());
log("age = %s", (dict["age"].asString()).c_str());
ValueVector& array = plist["array"].asValueVector();
for (int i = ; i < array.size(); i++) {
Value& value = array[i];
log("%d", value.asInt());
}
return true;
}
init
调试运行:
<data>
<p name="ZhangSan" age="10"></p>
<p name="LiSi" age="11"></p>
</data>
cocos2d-x内置了API操作xml,首先需要引入头文件:
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
} auto doc = new tinyxml2::XMLDocument();//首先需要创建文档
doc->Parse(FileUtils::getInstance()->getStringFromFile("data.xml").c_str());//解析字符串
auto root = doc->RootElement();//获取到根节点,根据根节点查找到子对象 //外层循环遍历所有的子项,内存循环遍历当前子项所有的属性
for (auto e = root->FirstChildElement(); e!=null; e = e->NextSiblingElement()){
std::string str;
for (auto attr = e->FirstAttribute(); attr; attr = attr->Next()){
str += attr->Name();
str += ":";
str += attr->Value();
str += ",";
}
log("%s",str.c_str());
} return true;
}
init
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
} rapidjson::Document doc;
//Parse<unsigned int parseFlags>(const Ch *str):
//parseFlags 指解析的方式,一般用默认的解析方式,只需要传入0就好了
doc.Parse<>(FileUtils::getInstance()->getStringFromFile("data.json").c_str());
log("%s", doc[(rapidjson::SizeType)]["name"].GetString());
return true;
}
init
五毛的cocos2d-x学习笔记07-计时器、数据读写、文件读写的更多相关文章
- 学习笔记 07 --- JUC集合
学习笔记 07 --- JUC集合 在讲JUC集合之前我们先总结一下Java的集合框架,主要包含Collection集合和Map类.Collection集合又能够划分为LIst和Set. 1. Lis ...
- 机器学习实战(Machine Learning in Action)学习笔记————07.使用Apriori算法进行关联分析
机器学习实战(Machine Learning in Action)学习笔记————07.使用Apriori算法进行关联分析 关键字:Apriori.关联规则挖掘.频繁项集作者:米仓山下时间:2018 ...
- Java学习笔记——File类之文件管理和读写操作、下载图片
Java学习笔记——File类之文件管理和读写操作.下载图片 File类的总结: 1.文件和文件夹的创建 2.文件的读取 3.文件的写入 4.文件的复制(字符流.字节流.处理流) 5.以图片地址下载图 ...
- 微信小程序开发:学习笔记[9]——本地数据缓存
微信小程序开发:学习笔记[9]——本地数据缓存 快速开始 说明 本地数据缓存是小程序存储在当前设备上硬盘上的数据,本地数据缓存有非常多的用途,我们可以利用本地数据缓存来存储用户在小程序上产生的操作,在 ...
- springmvc学习笔记(18)-json数据交互
springmvc学习笔记(18)-json数据交互 标签: springmvc springmvc学习笔记18-json数据交互 springmvc进行json交互 环境准备 加入json转换的依赖 ...
- Java NIO 学习笔记(六)----异步文件通道 AsynchronousFileChannel
目录: Java NIO 学习笔记(一)----概述,Channel/Buffer Java NIO 学习笔记(二)----聚集和分散,通道到通道 Java NIO 学习笔记(三)----Select ...
- OpenCV 学习笔记 07 目标检测与识别
目标检测与识别是计算机视觉中最常见的挑战之一.属于高级主题. 本章节将扩展目标检测的概念,首先探讨人脸识别技术,然后将该技术应用到显示生活中的各种目标检测. 1 目标检测与识别技术 为了与OpenCV ...
- [Golang学习笔记] 07 数组和切片
01-06回顾: Go语言开发环境配置, 常用源码文件写法, 程序实体(尤其是变量)及其相关各种概念和编程技巧: 类型推断,变量重声明,可重名变量,类型推断,类型转换,别名类型和潜在类型 数组: 数组 ...
- stm32寄存器版学习笔记07 ADC
STM32F103RCT有3个ADC,12位主逼近型模拟数字转换器,有18个通道,可测量16个外部和2个内部信号源.各通道的A/D转换可以单次.连续.扫描或间断模式执行. 1.通道选择 stm32把A ...
- [原创]java WEB学习笔记07:关于HTTP协议
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
随机推荐
- centos 6.7 perl 版本 This is perl 5, version 22 安装DBI DBD
<pre name="code" class="cpp">centos 6.7 perl 版本 This is perl 5, version 22 ...
- 圣何塞与 Microsoft 宣布该市为超过 5,000 名市府公务员选择 Office 365、Windows Azure 和 StorSimple
过去几个月来我们展示了极大的客户吸引力,今天我们非常高兴地宣布,我们又赢得了一位新客户,且他们利用 Microsoft 革新 IT 的方式非常有趣. 今天,我们非常高兴地告诉大家,圣何塞市选择了 Mi ...
- nodejs运用passport和passport-local分离本地登录
var express = require('express'); var cookieParser = require('cookie-parser'); var bodyParser = requ ...
- 【HTML5】DOMContentLoaded事件
这个事件是从HTML中的onLoad的延伸而来的,当一个页面完成加载时,初始化脚本的方法是使用load事件,但这个类函数的缺点是仅在所有资源都完全加载后才被触发,这有时会导致比较严重的延迟,开发人员随 ...
- CouldnotcreateServerSocketonaddress0.0.0.0/0.0.0.0:9083
错误记录 安装的时候遇到了如下错误 Exception in thread "main" org.apache.thrift.transport.TTransportExcepti ...
- PHP 操作redis 详细讲解转的
http://www.cnblogs.com/jackluo/p/3412670.html phpredis是redis的php的一个扩展,效率是相当高有链表排序功能,对创建内存级的模块业务关系 很有 ...
- poj 1149 PIGS(最大流经典构图)
题目描述:迈克在一个养猪场工作,养猪场里有M 个猪圈,每个猪圈都上了锁.由于迈克没有钥匙,所以他不能打开任何一个猪圈.要买猪的顾客一个接一个来到养猪场,每个顾客有一些猪圈的钥匙,而且他们要买一定数量的 ...
- Ubuntu 13.04 小米2S连接Eclipse真机调试
最近想继续将自己以前的一些Android程序代码进行改进和优化,遂将以前的代码在windows下导入eclipse工程,谁知导入后便eclipse假死,甚至windows资源管理器也动弹不得,诡异的是 ...
- ubuntu系统安装FTP
Ubuntu安装vsftp软件 1.更新软件源 首先须要更新系统的软件源,便捷工具下载地址:http://help.aliyun.com/manual?spm=0.0.0.0.zJ3dBU&h ...
- ##DAY14——StoryBoard
•iOS下可视化编程分为两种⽅式:xib和storyboard. •在使用xib和storyboard创建GUI过程中,以XML文件格式 存储在Xcode中,编译时生成nib的二进制文件.在运行时, ...