Cocos2d-x 捕鱼达人游戏学习教程(1)

静态数据模型的创建




本文中的资源都是来自互联网,这一系列文章只是用于大家学习交流,不能用作任何商业用途。




为了方便大家学习这里是本文的代码例子:Hi,推荐文件给你 "捕鱼达人练习例子(1).zip"http://vdisk.weibo.com/s/Ix9tE






我们首先,从捕鱼达人游戏中的数据模型入手开始学习。


游戏中的数据模型,分为静态模型和动态模型。




静态模型


静态数据就是游戏中的只读数据,包括游戏中的图片、文字数据等。




动态模型


动态数据就是游戏中会改变的数据,就像经验、金币等。




我们首先场景一个静态的数据模型类StaticData这个类继承自CCObject




StaticData.h








#include "cocos2d.h"
USING_NS_CC;
using namespace std;
#define STATIC_DATA_STRING(key) StaticData::sharedStaticData()->stringFormKey(key)
#define STATIC_DATA_INT(key)   StaticData::sharedStaticData()->intFromKey(key)
#define STATIC_DATA_FLOAT(key) StaticData::sharedStaticData()->floatFromKey(key)
#define STATIC_DATA_BOOLEAN(key) StaticData::sharedStaticData()->booleanFromKey(key)
#define STATIC_DATA_POINT(key)   StaticData::sharedStaticData()->pointFromKey(key)
#define STATIC_DATA_RECT(key)    StaticData::sharedStaticData()->rectFromKey(key)
#define STATIC_DATA_SIZE(key)   StaticData::sharedStaticData()->sizeFromKey(key)

class StaticData:public CCObject
{
    
public:
    //单例的初始化方法
    static StaticData* sharedStaticData();
    
    const char* stringFormKey(string key);
    int intFromKey(string key);
    float floatFromKey(string key);
    bool booleanFromKey(string key);
    CCPoint pointFromKey(string key);
    CCRect rectFromKey(string key);
    CCSize sizeFromKey(string key);
    
    
    //内存不足时调用
    void purge();
    
    CC_SYNTHESIZE_READONLY(string, _staticDataPath, StaticDataPath);
protected:
    CCDictionary* _dictionary;
private:
    //该数据模型的构造方法
    StaticData();
    //该数据模型的析构方法
    ~StaticData();
    bool init();
};









StaticData.cpp代码如下:






#include "StaticData.h"
//static_data.plist是静态数据的来源
#define STATIC_DATA_PATH "static_data.plist"

static StaticData* _sharedStaticData = NULL;

StaticData* StaticData::sharedStaticData()
{
    if (_sharedStaticData == NULL)
    {
        _sharedStaticData = new StaticData();
        _sharedStaticData->init();
    }
    return _sharedStaticData;
}





StaticData::StaticData()
{
    //在构造方法中将静态数据的数据源赋值给一个C++的字符串
    _staticDataPath = STATIC_DATA_PATH;
}

StaticData::~StaticData()
{
    //在析构方法中将这个字典安全释放
    CC_SAFE_RELEASE_NULL(_dictionary);
}





bool StaticData::init()
{
    //初始化一个字典,数据的
    _dictionary = CCDictionary::createWithContentsOfFile(_staticDataPath.c_str());
    //这个字典需要在当前这个类的其他方法中使用,因此需要retain一下,释放的代码写在析构方法中。
    CC_SAFE_RETAIN(_dictionary);
    return true;
}





//从字典中取值返回C的字符串的方法
const char* StaticData::stringFormKey(string key)
{
    return _dictionary->valueForKey(key)->getCString();
}
//从字典中取值返回int值的方法
int StaticData::intFromKey(string key)
{
    return _dictionary->valueForKey(key)->intValue();
}
//从字典中取值返回一个浮点数的方法
float StaticData::floatFromKey(string key)
{
    return _dictionary->valueForKey(key)->floatValue();
}

//从字典中取值返回一个bool值的方法
bool StaticData::booleanFromKey(string key)
{
    return _dictionary->valueForKey(key)->boolValue();
}
//从字典中取值返回一个CCPoint值的方法
CCPoint StaticData::pointFromKey(string key)
{
    //从字符串中取出一个CCPoint类型的数据
    return CCPointFromString(_dictionary->valueForKey(key)->getCString());
}
//从字典中取值返回一个CCRect值的方法
CCRect StaticData::rectFromKey(string key)
{
    //从字符串中取出一个CCRect类型的数据
    return CCRectFromString(_dictionary->valueForKey(key)->getCString());
}

//从字典中取值返回一个CCSize值的方法
CCSize StaticData::sizeFromKey(string key)
{
    //从字符串中取出一个CCSize类型的数据
    return CCSizeFromString(_dictionary->valueForKey(key)->getCString());
}

//内存不足时调用
void StaticData::purge()
{
     //使用release()方法释放Cocos2d-x对象p的一次引用,再把p赋值为NULL。如果p已经为NULL,则不进行操作  
    CC_SAFE_RELEASE_NULL(_sharedStaticData);
}



以上就是游戏中静态数据模型的代码。