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(); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Oracle中的控制文件(control file)
- ruby-on-rails – 如何使用Paperclip(或RMagick)进行裁剪和
- Oracle SQL Developer中的Excel日期字段导入问题
- c# – ASP.NET MVC应用程序的架构
- objective-c – SLComposeViewController CompletionHandle
- cocos2d-x3.2在mac os下制作编译.so提示ui错误解决办法
- Ruby on Rails. dispatch.fcgi. dependencies.rb:251:在`
- flex 学习笔记(摘自《Flex3 程序设计》)
- 在Xml文件配置时遇到不提示信息的解决方法
- c – 使用常量长度时char []和new char []之间的差异