2016年5月5日 星期四

0505 file save by cocos2d-x (andriod ios pc)

1.利用cocos2d-x 進行開檔 存檔範例
2. CC_SYNTHESIZE 使用範例
3.獨立的class 建立 getInstance() 範例

 .h
#ifndef __ROBOTFILESAVE__
#define __ROBOTFILESAVE__
#include <stdio.h>
#include "cocos2d.h"
using namespace cocos2d;
using namespace std;
const char G_FileName_Setting[48] ="AppSetting.ini";
class FileSetting :Node
{
private:
    string filePath;
public:
    CREATE_FUNC(FileSetting);
    virtual bool init() override;
    FileSetting();
    ~FileSetting();
    static FileSetting* getInstance();
    void LoadFileSetting();
    void SaveFileSetting();
public: //通用函數
    CC_SYNTHESIZE(int, timeSyncHint, TimeSyncHint);
    CC_SYNTHESIZE(int, stopActionHint, StopActionHint);
};

#endif //__ROBOTFILESAVE__

.cpp
 #include "Def_ImageList.h"
#include "Robot_FileSave.h"
//是否要提示顯示同步是窗
static FileSetting* fileSetting=nullptr;
FileSetting::FileSetting():
stopActionHint(0),
timeSyncHint(0)
{
   
    filePath = FileUtils::getInstance()->getWritablePath()+"\\"+G_FileName_Setting;
   
}
FileSetting::~FileSetting()
{
   
}
FileSetting* FileSetting::getInstance()
{
   
    if (!fileSetting) {
       
        fileSetting = new FileSetting;
       
        fileSetting->init();
       
    }
   
    return fileSetting;
   
}
bool FileSetting::init()
{
    if (!Node::init())
    {
        return false;
    }
    //載入設定
    LoadFileSetting();
    return true;
}
void FileSetting::SaveFileSetting()
{
    FILE *fp =fopen(filePath.c_str(),"w");
    if(fp!=NULL)
    {
        fprintf(fp,"TimeSyncHint = %d /n",timeSyncHint);
        fprintf(fp,"StopActionHint = %d /n",stopActionHint);
        fclose(fp);
    }
    else
    {
        log("open fail:%s ",filePath.c_str());
    }
}
void FileSetting::LoadFileSetting()
{
    FILE *fp =fopen(filePath.c_str(),"r");
    if(fp==NULL)
    { //全新紀錄 寫入新設定
        SaveFileSetting();
    }
    else
    { //載入設定
        fscanf(fp,"TimeSyncHint = %d /n",&timeSyncHint);
        fscanf(fp,"StopActionHint = %d /n",&stopActionHint);
        fclose(fp);
    }
}

沒有留言:

張貼留言

cocos2dx-lua 建立滑鼠監聽

重要關鍵字  EVENT_MOUSE_SCROLL addEventListenerWithSceneGraphPriority      if IsPc() then --建立滑鼠監聽         local listener = cc.EventListenerMouse...