//GameScene.h #include "cocos2d.h" USING_NS_CC; class GameScene : public cocos2d::Layer { public: static cocos2d::Scene* createScene(); virtual bool init(); void menuCallback(Ref* pSender); CREATE_FUNC(GameScene); private: //注意不能用auto关键字 Size size; Sprite *sprite; };
//GameScene.cpp // Created by Jacedy on 14-8-11. #include "GameScene.h" USING_NS_CC; cocos2d::Scene* GameScene::createScene() { auto scene = Scene::create(); //创建一个场景 auto layer = GameScene::create(); //创建一个图层 scene->addChild(layer); return scene; } //初始化当前的图层 bool GameScene::init() { if(!Layer::init()) //初始化父类 return false; //获取屏幕大小 size = Director::getInstance()->getVisibleSize(); //auto size = Director::getInstance()->getWinSize(); std::string fileName = FileUtils::getInstance()->getWritablePath() + "test.txt"; //获取写入文件的绝对路径 char szBuff[100] = "Hello Jacedy"; //向本地写入文件 FILE *fp = fopen(fileName.c_str(), "wb"); if (fp) { fwrite(szBuff, 1, strlen(szBuff), fp); fclose(fp); log("Write succeed"); } else log("Write failed"); //读取文件 std::string fullPath = FileUtils::getInstance()->fullPathForFilename("test.txt"); //获取读入文件的绝对路径 if(fileName.length() > 0) { fp = fopen(fileName.c_str(), "rb"); if (fp) { char szReadBuff[100] = {0}; size_t read = fread(szReadBuff, 1, strlen(szBuff), fp); if (read > 0) { log("The content of file: %s", szReadBuff); //在窗口显示 auto label = Label::createWithSystemFont("", "Marker Felt", 50); label->setPosition(Vec2(size.width/2, size.height/2)); this->addChild(label); label->setString(szReadBuff); } } } return true; } void GameScene::menuCallback(Ref* pSender) { }
Cocos2d-x3.2 文件读写
时间: 2024-10-06 08:41:17