加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Cocos2d-x_保存数据和读取数据

发布时间:2020-12-14 19:11:38 所属栏目:百科 来源:网络整理
导读://// HelloWorldScene.h//#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"#include "cocos-ext.h"USING_NS_CC;USING_NS_CC_EXT;class HelloWorld : public cocos2d::CCLayer{public: virtual bool init(); static cocos2
//
// HelloWorldScene.h
//

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "cocos-ext.h"

USING_NS_CC;
USING_NS_CC_EXT;

class HelloWorld : public cocos2d::CCLayer
{
public:
    virtual bool init();
    static cocos2d::CCScene* scene();

    CREATE_FUNC(HelloWorld);

    void saveGameData();
};

#endif

//
// HelloWorldScene.cpp
//

#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"

USING_NS_CC;
using namespace std;
using namespace CocosDenshion;


CCScene* HelloWorld::scene()
{
    CCScene *scene = CCScene::create();
    HelloWorld *layer = HelloWorld::create();
    scene->addChild(layer);
    
    return scene;
}

bool HelloWorld::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();

    CCLabelTTF *pLabelTTF = CCLabelTTF::create("","Helvetica",25);
    pLabelTTF->setPosition(ccp(winSize.width*0.5,winSize.height*0.5));
    this->addChild(pLabelTTF,90);
    
    this->saveGameData();
    
    return true;
}

void HelloWorld::saveGameData()
{
    CCLabelTTF *pttf = (CCLabelTTF *)this->getChildByTag(90);
    
    if (CCUserDefault::sharedUserDefault()->getBoolForKey("bool",false))
    {
        pttf->setString("有存档,开始读取数据!");
        
        // 读取数据
        bool iBool = CCUserDefault::sharedUserDefault()->getBoolForKey("bool",false);
        double iDouble = CCUserDefault::sharedUserDefault()->getDoubleForKey("double",0.0);
        float iFloat = CCUserDefault::sharedUserDefault()->getFloatForKey("float",0.0);
        int iInteger = CCUserDefault::sharedUserDefault()->getIntegerForKey("integer",0);
        string iString = CCUserDefault::sharedUserDefault()->getStringForKey("string","");
        
        CCLOG("iBool:%d,iDouble:%f,iFloat:%f,iInteger:%d,iString:%s",iBool,iDouble,iFloat,iInteger,iString.c_str());
    }
    else
    {
        pttf->setString("还没有存档,开始存档数据!");
        
        // 准备写入数据
        CCUserDefault::sharedUserDefault()->setBoolForKey("bool",true);
        CCUserDefault::sharedUserDefault()->setDoubleForKey("double",9.22);
        CCUserDefault::sharedUserDefault()->setFloatForKey("float",8.33);
        CCUserDefault::sharedUserDefault()->setIntegerForKey("integer",20);
        CCUserDefault::sharedUserDefault()->setStringForKey("string","cxm");
        
        // 提交数据
        CCUserDefault::sharedUserDefault()->flush();
    }
}



(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读