1.图片
USE_LOCAL_PATH(
CCSprite *temp=CCSprite::create(“temp.png”);
temp->setPosition(ccp(WIN_CENTER.x-150,280));
this->addChild(temp);
)
2.文字
CCLabelTTF *labTemp=CCLabelTTF::create(Common::getLocalString(“ss”), FONT_NAME, 25);
labTemp->setPosition(ccp(WIN_CENTER.x+150,280));
this->addChild(labTemp);
3.存储、读取
Common::saveDataByKeyValue(KEY_SAVE_PAGE_LEVEL, CCString::createWithFormat(“%d”,iSelectIndex));
iSelectIndex=Common::getDataByKey(KEY_SAVE_PAGE_LEVEL)->intValue();
组织目录
源码
Common.h
#ifndef Fire_Const_h
#define Fire_Const_h
#include “cocos2d.h”
#include “AppDelegate.h”
#include “DataBase64.h”
using namespace cocos2d;
#define USE_LOCAL_PATH(x) \
{ \
std::string sDirOld=CCFileUtils::sharedFileUtils()->getResourceDirectory(); \
std::string sDir=Common::getLocalPath(); \
sDir.append(sDirOld); \
CCFileUtils::sharedFileUtils()->setResourceDirectory(sDir.c_str()); \
x \
CCFileUtils::sharedFileUtils()->setResourceDirectory(sDirOld.c_str()); \
}
#define KEY_SAVE_CODE “easymobi”
static CCDictionary *dicLocalString=NULL;
class Common{
public:
static std::string getLocalPath(){
ccLanguageType lt= CCApplication::sharedApplication()->getCurrentLanguage();
std::string sDir;
switch (lt) {
case cocos2d::kLanguageChinese:
sDir=”language/zh/”;
break;
default:
sDir=”language/en/”;
break;
}
return sDir;
}
static const char* getLocalString(const char *sKey){
if(dicLocalString==NULL){
std::string sDirOld=CCFileUtils::sharedFileUtils()->getResourceDirectory();
std::string sDir=getLocalPath();
CCFileUtils::sharedFileUtils()->setResourceDirectory(sDir.c_str());
dicLocalString=CCDictionary::createWithContentsOfFile(“LocalString.plist”);
dicLocalString->retain();
CCFileUtils::sharedFileUtils()->setResourceDirectory(sDirOld.c_str());
}
return ((CCString *)dicLocalString->objectForKey(sKey))->getCString();
}
static void purgeDicLocalString(){
if(dicLocalString!=NULL){
dicLocalString->release();
}
}
static CCArray* stringSplit(const char *str,const char *delim){
char *p;
char *s=(char *)CCString::create(str)->getCString();
char *d=(char *)CCString::create(delim)->getCString();
p = strtok(s, d);
CCArray *arrTemp = CCArray::create();
while (p){
CCString *sObj = CCString::create(p);
arrTemp->addObject(sObj);
p = strtok(NULL, delim);
}
return arrTemp;
}
static void saveDataByKeyValue(const char *sKey,CCString *sValue){
char buffer[32];
sprintf(buffer, “%s%s”, KEY_SAVE_CODE,sKey);
std::string sRealKey = saveData(reinterpret_cast<const unsigned char*>(sValue->getCString()), sValue->length());
CCUserDefault::sharedUserDefault()->setStringForKey(buffer, sRealKey);
CCUserDefault::sharedUserDefault()->flush();
}
static CCString* getDataByKey(const char *sKey){
char buffer[32];
sprintf(buffer, “%s%s”, KEY_SAVE_CODE,sKey);
std::string s = CCUserDefault::sharedUserDefault()->getStringForKey(buffer);
std::string parseKey = parseData(s);
return CCString::create(parseKey);
}
};
#endif
DataBase64.h
#ifndef __Truck__DataBase64__
#define __Truck__DataBase64__
#include <string>
std::string saveData(unsigned char const* , unsigned int len);
std::string parseData(std::string const& s);
#endif
DataBase64.cpp
#include “DataBase64.h”
static const std::string dataChars =
“ABCDEFGHIJKLMNOPQRSTUVWXYZ”
“abcdefghijklmnopqrstuvwxyz”
“0123456789+/”;
static inline bool baseData(unsigned char c) {
return (isalnum(c) || (c == ‘+’) || (c == ‘/’));
}
std::string saveData(unsigned char const* bytes_to_encode, unsigned int in_len) {
std::string ret;
int i = 0;
int j = 0;
unsigned char char_array_3[3];
unsigned char char_array_4[4];
while (in_len–) {
char_array_3[i++] = *(bytes_to_encode++);
if (i == 3) {
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0×03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for(i = 0; (i <4) ; i++)
ret += dataChars[char_array_4[i]];
i = 0;
}
}
if (i)
{
for(j = i; j < 3; j++)
char_array_3[j] = ‘\0′;
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0×03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (j = 0; (j < i + 1); j++)
ret += dataChars[char_array_4[j]];
while((i++ < 3))
ret += ‘=’;
}
return ret;
}
std::string parseData(std::string const& encoded_string) {
int in_len = encoded_string.size();
int i = 0;
int j = 0;
int in_ = 0;
unsigned char char_array_4[4], char_array_3[3];
std::string ret;
while (in_len– && ( encoded_string[in_] != ‘=’) && baseData(encoded_string[in_])) {
char_array_4[i++] = encoded_string[in_]; in_++;
if (i ==4) {
for (i = 0; i <4; i++)
char_array_4[i] = dataChars.find(char_array_4[i]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0×30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0×3) << 6) + char_array_4[3];
for (i = 0; (i < 3); i++)
ret += char_array_3[i];
i = 0;
}
}
if (i) {
for (j = i; j <4; j++)
char_array_4[j] = 0;
for (j = 0; j <4; j++)
char_array_4[j] = dataChars.find(char_array_4[j]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0×30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0×3) << 6) + char_array_4[3];
for (j = 0; (j < i – 1); j++) ret += char_array_3[j];
}
return ret;
}