#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"USING_NS_CC;class HelloWorld : public cocos2d::Layer{public: static cocos2d::Scene* createScene(); virtual bool init(); // 使用Cocos封装好的API进行写入 voID saveDataByCocosAPI(); // 使用Cocos封装好的API进行读取 voID readDataByCocosAPI(); // 使用自定义的API进行写入 voID saveDataByCustomAPI(); // 使用自定义的API进行读取 voID readDataByCustomAPI(); CREATE_FUNC(HelloWorld);};#endif // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h"Scene* HelloWorld::createScene(){ auto scene = Scene::create(); auto layer = HelloWorld::create(); scene->addChild(layer); return scene;}bool HelloWorld::init(){ if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); auto sprite = Sprite::create("HelloWorld.png"); sprite->setposition(Vec2(visibleSize.wIDth / 2 + origin.x,visibleSize.height / 2 + origin.y)); this->addChild(sprite,0); this->saveDataByCocosAPI(); this->readDataByCocosAPI(); this->saveDataByCustomAPI(); this->readDataByCustomAPI(); return true;}voID HelloWorld::saveDataByCocosAPI(){ UserDefault::getInstance()->setBoolForKey("bool",false); UserDefault::getInstance()->setDoubleForKey("double",1.00f); UserDefault::getInstance()->setfloatForKey("float",1.0f); UserDefault::getInstance()->setIntegerForKey("integer",100); UserDefault::getInstance()->setStringForKey("string","Hello"); UserDefault::getInstance()->flush();}voID HelloWorld::readDataByCocosAPI(){ bool iBool = UserDefault::getInstance()->getBoolForKey("bool",false); double IDouble = UserDefault::getInstance()->getDoubleForKey("double",0.00f); float ifloat = UserDefault::getInstance()->getfloatForKey("float",0.0f); int iInteger = UserDefault::getInstance()->getIntegerForKey("integer",0); std::string iString = UserDefault::getInstance()->getStringForKey("string",""); log("iBool:%d,IDouble:%f,ifloat:%f,iInteger:%d,iString:%s",iBool,IDouble,ifloat,iInteger,iString.c_str());}voID HelloWorld::saveDataByCustomAPI(){ log("%s",fileUtils::getInstance()->getWritablePath().c_str()); auto fileUtils = fileUtils::getInstance(); file *fp = fopen(fileUtils->fullPathFromrelativefile("data.txt",fileUtils->getWritablePath()).c_str(),"w"); fprintf(fp,"Hello World\n"); fclose(fp);}voID HelloWorld::readDataByCustomAPI(){ auto fileUtils = fileUtils::getInstance(); std::string str = fileUtils->getStringFromfile(fileUtils->fullPathFromrelativefile("data.txt",fileUtils->getWritablePath())); log("str:%s",str.c_str());}总结
以上是内存溢出为你收集整理的Cocos2d-3.x_保存数据和读取数据全部内容,希望文章能够帮你解决Cocos2d-3.x_保存数据和读取数据所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)