Generally, you need to put the new class in the classes folder. This is better for porting. However, the folder cannot be directly specified when VS is directly created, so it took some time to create a script in the afternoon. First, use python to write the file creation function. The interface is simple: Project name and new class name. Because you want to create it in the cocos2d-x/projects folder ., The py file and the subsequent bat file should be placed under the cocos2d-x root directory: py code:
import sys,os def createFileInClass(projectName,newClassName): projectDir = "projects\\"+projectName+"\\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 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() createFileInClass(options.projectName,options.newClassName)
It is easy to save it as CreateClassInProject. py, but it still takes some time because I have not written python code before. Then 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% echo Create successfully,Please add it in Classes.
Save pause as CreateClassInProject. bat