cocos2d-x 資料存放區

來源:互聯網
上載者:User

1. 用CCUserDefault儲存簡單資料   

CCUserDefault *userDefault =CCUserDefault::sharedUserDefault();

    

    if(!userDefault->getBoolForKey("isExisted")){

        userDefault->setBoolForKey("isExisted",true);//不知道userDefault是啥?本文第五行,自己翻去

    }

    

    string user = userDefault->getStringForKey("user","");

    

    if (user == "") {

        userDefault->setStringForKey("user","allen");

        userDefault->flush();// save data between sessions 就是說可以在關閉遊戲後重新開啟遊戲時仍然可以讀取資料

        CCLog(CCString::createWithFormat("user not exsit, create player 'allen'
.")->getCString());

        pLabel->setString(CCString::createWithFormat("user not exsit, create player 'allen' .")->getCString());

    }else {

        string user = userDefault->getStringForKey("user","");

        CCLog(CCString::createWithFormat("get user exsit!!")->getCString());

        pLabel->setString(CCString::createWithFormat("get user exsited!!")->getCString());

    }

2. 用sqlite3 儲存複雜資料

見這個文章:

http://article.ityran.com/archives/2793

但在用xcode 進行c++開發時需注意2點:

a.只需要copy sqlite3.h, sqlite3.c 和sqlite3ext.h,不要拷貝shell.h。否則編譯會出錯

b.在建立資料庫的時候win32不需要指定路徑,而xcode開發中需要指定路徑:

   // test SQLite3 database

    sqlite3 *pDB = NULL;

    char *errMsg = NULL;

    string sqlStr;

    int result;

    

    // open db

    string sqlPath =
CCFileUtils::sharedFileUtils()->getWriteablePath() +
"save.db";

    CCLog(sqlPath.c_str());

    result = sqlite3_open_v2(sqlPath.c_str(), &pDB,
SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE,
NULL);

    if (result != SQLITE_OK) {

        CCLog("open database failed, error code : %d, error message : %s\n", result, errMsg);

    }

    

    // create table

    result = sqlite3_exec(pDB,
"create table MyTable_1(ID integer primary key autoincrement, name nvarchar(32)) if not exsit",
NULL, NULL, &errMsg);

    if (result != SQLITE_OK) {

        CCLog("create table failed, error code : %d, error message : %s\n", result, errMsg);

    }

    

    // insert data

    result = sqlite3_exec(pDB,
" insert into MyTable_1( name ) values ('allen')",
NULL, NULL, &errMsg);

    if (result != SQLITE_OK) {

        CCLog("insert data failed, error code : %d, error message : %s\n", result, errMsg);

    }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.