cocos2d-x 3.0 利用python脚本在文件夹Classes内创建class

时间:2021-03-09 18:17:37

因为VS2012创建默认文件是在proj.win32下,新建类不在VS的classes于是编译时找不到类。直接写个脚本帮助新建类(cpp和h文件),还能够在里面加上一些预先写好的代码。

批处理文件CreateClassInProject.bat,代码例如以下

@echo off
echo CreateClassInProject:
set /p projectName=Please Input Project Name:
set /p className=Please Input Class Name: echo Creating... E:\cocos2d-x-3.0\CreateClassInProject.py -p %projectName% -n %className%
CreateClassInProject.py -p HelloCocoStudio -n %className% echo Create successfully,Please add it in Classes. pause

还有一个是python文件CreateClassInProject.py,我装的是python2.7.6,代码例如以下

import sys,os  

def createFileInClass(projectName,newClassName):
print("001...");
projectDir = "projects\\"+projectName+"\\Classes";
#projectDir = "Classes";
os.chdir(projectDir);
headName=newClassName+".h";
sourceName=newClassName+".cpp" headFile=open(headName,"a");
tempFileName = "_"+newClassName.upper()+"_H_"; headFile.write("#ifndef "+ tempFileName +"\n#define "+tempFileName+"\n\n")
headFile.write("#include \"cocos2d.h\""+"\n\n");
headFile.write("using namespace cocos2d;\n\n");
headFile.write("class "+newClassName+"\n"+"{"+"\n"+"};"+"\n\n"+"#endif");
headFile.close(); sourceFile = open(sourceName,"a");
sourceFile.write("#include "+"\""+headName+"\"");
sourceFile.close() from optparse import OptionParser print("002...");
parser = OptionParser()
parser.add_option("-p", "--dir", dest="projectName",help="the project name",)
parser.add_option("-n", "--new", dest="newClassName", help="new class")
(options, args) = parser.parse_args() print("john...");
createFileInClass(options.projectName,options.newClassName)

把这两个文件放在cocos2dx的根文件夹下,由于代码里设置的路径是cocos2dx根文件夹下的projects.然后在桌面生成一个CreateClassInProject.bat的快捷方式,从桌面点击就能够了。有什么问题能够留言。cocos2d-x 3.0 利用python脚本在文件夹Classes内创建class