转载请说明出处:http://blog.csdn.net/lsmfeixiang/article/details/43706551
github地址:https://github.com/teffy/cocos2dx
年前已经把这部分东西学完了,但是后面出现了一个bug,紧接着公司组织关系变更,搬家到新的地方上班等事情,忙完接着就回家过年了。终于,年后开始上班了,抽出空来把blog写一下。
cocos2dx中数据存储的几种方式
1、userdefault
<span style="font-size:18px;">UserDefault是个微型数据库,你可以将基础数据类型存储在里面或从里面读取出来.
例如:setBoolForKey("played", true)是将一个bool值存储进去, 其key是"played",因此你可以通过getBoolForKey("played")从数据库中读取该bool值
其支持的基础数据类型如下: bool, int, float, double, string</span>
它的基本方法和android的sharedpreference很相似,不过android的需要在设置完值之后.edit().commit()一下才能真正的保存数据
UserDefault::getInstance()->setIntegerForKey("key", 122);
int intkey = UserDefault::getInstance()->getIntegerForKey("key");
log("UserDefault intkey: %d", intkey);
cocos2dx的场景切换不像android的activity跳转可以通过intent传递参数过去,那可以利用这个轻量级的数据存储来实现参数传递
2、plist文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>name</key>
<string>lumeng</string>
<key>age</key>
<integer>36</integer>
<key>family</key>
<dict>
<key>son</key>
<dict>
<key>name</key>
<string>xxx</string>
<key>age</key>
<integer>6</integer>
</dict>
<key>daughter</key>
<dict>
<key>name</key>
<string>yyy</string>
<key>age</key>
<integer>3</integer>
</dict>
</dict>
</dict>
</plist>
具体使用要用到Dictionary类
Dictionary* dict = Dictionary::createWithContentsOfFile("res/aaa.plist");//加载plist文件,转化为Dictionary对象
const __String* ss = dict->valueForKey("name");//根据key获取值
log("Plist name->%s", ss->getCString());
Ref* family = dict->objectForKey("family");
Dictionary* dictfamily = (Dictionary*) family;//根据key获取的值依然是一个dictionary,还需要继续解析
Dictionary* son = (Dictionary*) dictfamily->objectForKey("son");
const __String* sonname = son->valueForKey("name");
log("Plist sonname->%s", sonname->getCString());
3、sqlite3
sqlite3相信都不陌生,是一个轻量级的数据库,在移动平台上比较流行,在android上的数据库也是基于sqlite3,那在cocos2dx上用sqlite3要用到sqlite3的库,用法:
1、sqlite3.c&sqlite3.h&sqlite3ext.h,将这三个文件放入Classes文件夹中
2、win平台要在vs中将这三个文件加入进来
3、加入数据库处理的code
#include "sqlite3.h"
std::string path;
bool initdb() {
sqlite3* msqlite = NULL;
path = FileUtils::getInstance()->fullPathForFilename(DBNAME);
//在android上需要使用FileUtils获取一个可读写的路径,
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
path = FileUtils::getInstance()->getWritablePath();
path += "/";
path += DBNAME;
FILE* dbfile = fopen(path.c_str(),"r");
if (dbfile == nullptr) {
ssize_t size;
const char* data = (char*)FileUtils::getInstance()->getFileData(DBNAME, "rb", &size);
dbfile = fopen(path.c_str(), "wb");
fwrite(data, size, 1, dbfile);
CC_SAFE_DELETE_ARRAY(data);
}
fclose(dbfile);
#endif
log("dbpath->%s", path.c_str());
int open = sqlite3_open(path.c_str(), &msqlite);//打开数据库
if (open != SQLITE_OK) {//判断是否可以打开
const char* errmsg = sqlite3_errmsg(msqlite);
log("errmsg:%s", errmsg);
return false;
}
int result =
sqlite3_exec(msqlite,
"CREATE TABLE peoples (id integer primary key AUTOINCREMENT,name text,pwd text)",
NULL, NULL, NULL);//执行sql语句
log("1result:%d", result);
result = sqlite3_exec(msqlite,
"insert into peoples values(NULL,'lam','123334')", NULL, NULL,
NULL);
result = sqlite3_exec(msqlite,
"insert into peoples values(NULL,'wj','321')", NULL, NULL, NULL);
log("2result:%d", result);
sqlite3_close(msqlite);//关闭数据库
return true;
}
int sqlite3result(void* key, int c, char** value, char** cols) {//sqlite的回调函数,每查询出一行结果就会回调一次,c是这一行的列数,key是cols,value是value
log("c=%d", c);
for (int i = 0; i < c; i++) {
log("%s=%s", cols[i], value[i]);
}
return 0;
}
void HelloWorld::menuSqlite3Callback(Ref* pSender) {
sqlite3* msqlite;
int open = sqlite3_open(path.c_str(), &msqlite);
int result = sqlite3_exec(msqlite, "SELECT * FROM peoples", sqlite3result,
NULL, NULL);//执行sql语句,如果需要回调,那就需要传递一个回调函数的名字,比如<span style="font-family: Arial, Helvetica, sans-serif;">sqlite3result</span>
log("Sqlite3 result:%d", result);
sqlite3_close(msqlite);
}
打印结果:
c=3
id=1
name=lam
pwd=123334
c=3
id=2
name=wj
pwd=321
Sqlite3 result:0
在android设备上db文件存储路径,打印的log:
02-11 19:21:28.319: D/cocos2d-x debug info(10954): dbpath->/data/data/com.teffy.readdata/files//mysqlite3.db
学习的时候看到一篇对sqlite3的封装的文章,看起来不错:http://blog.csdn.net/ym19860303/article/details/8531998
这一部分的学习就先到这里了,下一篇准备学习一下网络传输的知识