重写一个CCBI的加载类

直接上源代码
//
//  CCBILoader.h
//  DueWest
//
//  Created by zxh on 13-7-17.
//
//


#ifndef __DueWest__CCBILoader__
#define __DueWest__CCBILoader__


#include "cocos2d.h"
#include "cocos-ext.h"


//定义模板加载类
template<class T>
class CCBILoader : public cocos2d::extension::CCLayerLoader
{
public:
    CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(CCBILoader<T>, loader);
protected:
    CCB_VIRTUAL_NEW_AUTORELEASE_CREATECCNODE_METHOD(T);
};


//定义加载宏
#define AUTO_CCBI_LOADER(TYPE) CCBILoader<TYPE>::loader()


//模板函数用加载ccbi文件
template<class T>
inline cocos2d::CCNode* ccbiLoadNode(const char* fileName, const char* className, cocos2d::CCNode* pOwner)
{


    cocos2d::extension::CCNodeLoaderLibrary* pLoaderLibary = cocos2d::extension::CCNodeLoaderLibrary::newDefaultCCNodeLoaderLibrary();
    pLoaderLibary->registerCCNodeLoader(className, AUTO_CCBI_LOADER(T));
    cocos2d::extension::CCBReader* reader = new cocos2d::extension::CCBReader(pLoaderLibary);
    cocos2d::CCNode* pNode = reader->readNodeGraphFromFile(fileName, pOwner);
    reader->autorelease();
    return pNode;
}


template<class T>
inline cocos2d::CCScene* ccbiCreateScene(const char* fileName, const char* className, cocos2d::CCNode* pOwner = NULL)
{
    
    cocos2d::extension::CCNodeLoaderLibrary* pLoaderLibary = cocos2d::extension::CCNodeLoaderLibrary::newDefaultCCNodeLoaderLibrary();
    pLoaderLibary->registerCCNodeLoader(className, AUTO_CCBI_LOADER(T));
    cocos2d::extension::CCBReader* reader = new cocos2d::extension::CCBReader(pLoaderLibary);
    cocos2d::CCScene* pScene = reader->createSceneWithNodeGraphFromFile(fileName, pOwner);
    reader->autorelease();
    return pScene;
}


#define AUTO_CCBI_CREATE_SCENE(type,fileName) ccbiCreateScene<type>(fileName, #type, NULL)


#endif /* defined(__DueWest__CCBILoader__) */


上面是一个纯头文件,最后直接使用:AUTO_CCBI_CREATE_SCENE宏即可,下面是使用方法:

CCScene* PreBattleScene::scene()
{
    CCScene* pScene = AUTO_CCBI_CREATE_SCENE(PreBattleScene, "Images/PreBattleScene.ccbi");
    return pScene;
}

不用再去定义蛋疼的Loader类了



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值