新建工程,testData
修改HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::CCLayer
{
public:
// Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
virtual bool init();
// there's no 'id' in cpp, so we recommend to return the class instance pointer
static cocos2d::CCScene* scene();
// preprocessor macro for "static create()" constructor ( node() deprecated )
CREATE_FUNC(HelloWorld);
};
#endif // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
using namespace cocos2d;
using namespace CocosDenshion;
CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
CCLabelTTF *ttf=CCLabelTTF::create("", "Helvetica", 23);
ttf->setPosition(ccp(240,160));
addChild(ttf);
if (CCUserDefault::sharedUserDefault()->getBoolForKey("bool",false)) {
ttf->setString("有存档,开始读取数据");
//Load
bool iBool=CCUserDefault::sharedUserDefault()->getBoolForKey("bool",false);
double iDouble=CCUserDefault::sharedUserDefault()->getDoubleForKey("double",0.0);
float iFloat=CCUserDefault::sharedUserDefault()->getFloatForKey("float",0.0);
int iInt=CCUserDefault::sharedUserDefault()->getIntegerForKey("int",0);
std::string iString=CCUserDefault::sharedUserDefault()->getStringForKey("string","");
CCLOG("%d_%f_%f_%d_%s",iBool,iDouble,iFloat,iInt,iString.c_str());
}
else
{
ttf->setString("还没有存档,开始存档数据");
CCUserDefault::sharedUserDefault()->setBoolForKey("bool", true);
CCUserDefault::sharedUserDefault()->setDoubleForKey("double", 9.22);
CCUserDefault::sharedUserDefault()->setFloatForKey("float", 8.23);
CCUserDefault::sharedUserDefault()->setIntegerForKey("int", 2);
CCUserDefault::sharedUserDefault()->setStringForKey("string", "Himi");
//提交
CCUserDefault::sharedUserDefault()->flush();
}
return true;
}