代码改变世界

XCode5添加新建类模板(Cocos2dx Template Class for Scene or Layer)

2014-04-04 21:15  张智清  阅读(1558)  评论(0编辑  收藏  举报

注:以下内容截选自Cocos2D开发网–Cocos2Dev.com,谢谢!

怎么添加xcode类模板?

1、打开Xcode的类模板目录地址:/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates

2、打开里面C and C++文件,就会看到Xcode的cpp类模板文件。复制一个C++ Class.xctemplate文件夹,取名:2dx Scene Class.xctemplate,放在C and C++目录下。

3、修改TemplateInfo.plist(主要修改创建文件时候的描述介绍)

打开2dx Scene Class.xctemplate/TemplateInfo.plist,   修改Description的值为:A C++ class for Cocos2d-x Scene。

4、修改___FILEBASENAME___.h

打开2dx Scene Class.xctemplate/___FILEBASENAME___.h,修改成以下内容:

//
// ___FILENAME___
// ___PROJECTNAME___
//
// Created by ___FULLUSERNAME___ on ___DATE___.
//___COPYRIGHT___
//

#ifndef _____PROJECTNAMEASIDENTIFIER________FILEBASENAMEASIDENTIFIER_____
#define _____PROJECTNAMEASIDENTIFIER________FILEBASENAMEASIDENTIFIER_____

#include "cocos2d.h"
USING_NS_CC;

class ___FILEBASENAMEASIDENTIFIER___ : public Layer
{
public:
___FILEBASENAMEASIDENTIFIER___();
~___FILEBASENAMEASIDENTIFIER___();
static Scene* createScene();
virtual bool init();
CREATE_FUNC(___FILEBASENAMEASIDENTIFIER___);

private:
void initData();
};

#endif /* defined(_____PROJECTNAMEASIDENTIFIER________FILEBASENAMEASIDENTIFIER_____) */

5、修改___FILEBASENAME___.cpp

打开2dx Scene Class.xctemplate/___FILEBASENAME___.cpp,修改成以下内容:

//
// ___FILENAME___
// ___PROJECTNAME___
//
// Created by ___FULLUSERNAME___ on ___DATE___.
//___COPYRIGHT___
//

#include "___FILEBASENAME___.h"
___FILEBASENAMEASIDENTIFIER___::___FILEBASENAMEASIDENTIFIER___()
{

}

___FILEBASENAMEASIDENTIFIER___::~___FILEBASENAMEASIDENTIFIER___()
{

}

Scene* ___FILEBASENAMEASIDENTIFIER___::createScene()
{
auto scene = Scene::create();
auto layer = ___FILEBASENAMEASIDENTIFIER___::create();
scene->addChild(layer);
return scene;
}

bool ___FILEBASENAMEASIDENTIFIER___::init()
{
if ( !Layer::init() )
{
return false;
}
initData();
return true;
}

#pragma mark - initData
void ___FILEBASENAMEASIDENTIFIER___::initData()
{
Size visibleSize = Director::getInstance()->getVisibleSize();

// add your codes here...

}

ok, 这样就完成了一个类模板的添加。现在在XCode里面就可新建一个2dx Scene类看看。

对应的你可以添加Layer的类模板。方法一样,我就不重复了。

关于文件模板里面的一个预定义变量,介绍下: