//
// Level.h
// Guess
//
// Created by zctech on 14-3-4.
//
//
#ifndef __Guess__Level__
#define __Guess__Level__
#include <iostream>
#include "cocos2d.h"
using namespace cocos2d;
//关卡类
class Level : public CCObject{
public:
//构造函数. filename:文件名, unlocked:是否解锁, anwer:答案
Level(int Id, std::string filename[4], bool unlocked, std::string anser);
//析构函数
~Level();
//get方法.
std::string getFileName(unsigned int index);
std::string getAnser();
bool getUnlocked();
int getId();
private:
std::string m_filename[4]; //文件名
std::string m_answer;
bool m_unlocked; //是否解锁
int m_id;
};
#endif /* defined(__Guess__Level__) */
#include "Level.h"
//构造函数. filename:文件名, unlocked:是否解锁, anwer:答案
Level::Level(int Id, std::string filename[4], bool unlocked, std::string answer)
{
for (unsigned int i = 0; i < 4; i++) {
m_filename[i] = filename[i];
}
m_id = Id;
m_unlocked = unlocked;
m_answer = answer;
}
//析构函数
Level::~Level()
{
}
//get方法.
std::string Level::getFileName(unsigned int index)
{
return m_filename[index];
}
std::string Level::getAnser()
{
return m_answer;
}
bool Level::getUnlocked()
{
return m_unlocked;
}
int Level::getId()
{
return m_id;
}
//
// LevelManager.h
// Guess
//
// Created by zctech on 14-3-4.
//
//
#ifndef __Guess__LevelManager__
#define __Guess__LevelManager__
#include <iostream>
#include "Level.h"
#include "CResource.h"
class LevelManager
{
public:
//构造函数
LevelManager();
//析构函数
~LevelManager();
//添加一个关卡. level:关卡
void addLevel(Level *level);
//返回指定位置的关卡. index:索引
Level *levelAtIndex(int index);
//返回元素个数
int count();
//以xml文件初始化.xmlName:xml文件
void initWithXml(const char *xmlName);
//修改关卡xml文档
void modifyLevelXml(const char *xmlName, int ID, bool unlock);
static LevelManager *sharedManager();
private:
cocos2d::CCArray *arrayLevel; //关卡数组
RcLevelSceneScroll m_rc;
static LevelManager *m_inst;
};
#endif /* defined(__Guess__LevelManager__) */
//
// LevelManager.cpp
// Guess
//
// Created by zctech on 14-3-4.
//
//
#include "LevelManager.h"
#include "tinyxml.h"
#include "tinystr.h"
using namespace std;
USING_NS_CC;
LevelManager * LevelManager::m_inst = NULL;
LevelManager *LevelManager::sharedManager()
{
if(!m_inst)
{
m_inst = new LevelManager();
m_inst->initWithXml(m_inst->m_rc.m_levelXml.c_str());
return m_inst;
}
return m_inst;
}
//构造函数
LevelManager::LevelManager(){
this->arrayLevel = new CCArray();
}
//析构函数
LevelManager::~LevelManager(){
this->arrayLevel->release();
}
//添加一个关卡. level:关卡
void LevelManager::addLevel(Level *level){
this->arrayLevel->addObject(level);
}
//返回指定位置的关卡. index:索引
Level * LevelManager::levelAtIndex(int index){
if(index < 0 || index >= this->arrayLevel->count())
{return NULL;}
return (Level *)this->arrayLevel->objectAtIndex(index);
}
//返回元素个数
int LevelManager::count(){
return this->arrayLevel->count();
}
//以xml文件初始化.xmlName:xml文件
void LevelManager::initWithXml(const char *xmlName){
string filePath = CCFileUtils::sharedFileUtils()->fullPathForFilename(xmlName);
TiXmlDocument *myDoc = new TiXmlDocument(filePath.c_str());
myDoc->LoadFile();
TiXmlElement *RootElement = myDoc->RootElement();
TiXmlElement *element = RootElement->FirstChildElement();
//遍历xml结点
while (element) {
TiXmlElement *id = element->FirstChildElement();
TiXmlElement *unlock = id->NextSiblingElement();
TiXmlElement *answer = unlock->NextSiblingElement();
TiXmlElement *filename1 = answer->NextSiblingElement();
TiXmlElement *filename2 = filename1->NextSiblingElement();
TiXmlElement *filename3 = filename2->NextSiblingElement();
TiXmlElement *filename4 = filename3->NextSiblingElement();
//获取文件名
string filename[4] = {filename1->FirstChild()->Value(), filename2->FirstChild()->Value(), filename3->FirstChild()->Value(), filename4->FirstChild()->Value()};
//获取id
int Id = atoi(id->FirstChild()->Value());
//获取是否解锁
bool Unlock = atoi(unlock->FirstChild()->Value());
//获取答案
string Answer = answer->FirstChild()->Value();
Level *level = new Level(Id, filename, Unlock, Answer);
this->addLevel(level);
element=element->NextSiblingElement();
}
}
//修改关卡xml文档
void LevelManager::modifyLevelXml(const char *xmlName, int ID, bool unlock)
{
string filePath = CCFileUtils::sharedFileUtils()->fullPathForFilename(xmlName);
TiXmlDocument *myDoc = new TiXmlDocument(filePath.c_str());
myDoc->LoadFile();
TiXmlElement *RootElement = myDoc->RootElement();
TiXmlElement *element = RootElement->FirstChildElement();
//遍历xml结点
while (element) {
TiXmlElement *id = element->FirstChildElement();
TiXmlElement *unlock = id->NextSiblingElement();
int Id = atoi(id->FirstChild()->Value());
//修改
if(Id == ID)
{
string str = unlock ? "1" : "0";
TiXmlText newText(str.c_str());
unlock->ReplaceChild(unlock->FirstChild(), newText);
myDoc->SaveFile();
}
element=element->NextSiblingElement();
}
}
level.xml
<root>
<level>
<id>0</id>
<unlocked>1</unlocked>
<answer>spring</answer>
<filename1>flower.png</filename1>
<filename2>flower.png</filename2>
<filename3>flower.png</filename3>
<filename4>flower.png</filename4>
</level>
<level>
<id>1</id>
<unlocked>1</unlocked>
<answer>summer</answer>
<filename1>summer_1.png</filename1>
<filename2>summer_2.png</filename2>
<filename3>summer_3.png</filename3>
<filename4>summer_4.png</filename4>
</level>
<level>
<id>2</id>
<unlocked>0</unlocked>
<answer>summer</answer>
<filename1>summer_1.png</filename1>
<filename2>summer_2.png</filename2>
<filename3>summer_3.png</filename3>
<filename4>summer_4.png</filename4>
</level>
<level>
<id>3</id>
<unlocked>0</unlocked>
<answer>summer</answer>
<filename1>summer_1.png</filename1>
<filename2>summer_2.png</filename2>
<filename3>summer_3.png</filename3>
<filename4>summer_4.png</filename4>
</level>
</root>
tinyXml文件夹http://pan.baidu.com/s/1hq1IACs