用cocos2d-x做开发的话,用的最多的开发语言就是c++/lua了,而现在公司很多都比较看重开发速度,较多的公司都选择了lua作为开发语言,同时lua的热更新也比c++的热更新更为便捷,更让更多的开发者都开始使用了lua进行开发。
作为手游开发者,自然不能落后^ ^。
开发工具的选择:sublime Text 。这里不选择coco官方的cocos code IDE 的理由是:api,各个方面插件功能支持不完善,基于eclpise的(之前在ADT遇到的种种问题,让我非常不爽,所以不喜欢!),内存占用问题(我电脑开着还想干其他事呢,怎么能卡呢? pass!),界面不简洁,不美观。然后么,基本上cocos code IDE 的不足之处都是sublime text的优势咯。
sublime 有功能众多的插件帮助开发者使用,界面精简,美观,占用内存小,开发速度快。而其中的QuickXDev插件,在基于quick的开发中,显得功能十分强大。
首先基本配置,选择QuickXDev 的setting-user:(以下是我本地的配置)
{
"quick_cocos2dx_root": "D:/cocos/quick-3.3",
"cocos2dx_root": "D:/cocos/quick-3.3",
"author": "cjsjy123",
"compile_scripts_key": "encrypt_key"
}
功能:
1.go to Definition,使其能够像IDE自由进行调整,查看这些api的具体实现和文档注释。
2.run with player ,快捷的启动player,查看运行效果。(这里可能会有问题,因为文档跳转用到了 quick_cocos2dx_root 和cocos2dx_root的环境变量配置,如果在这里修改使之能运行player,则将无法正确跳转,所以在quickx.py(在sublime 打开brower packages ,找到QuickXDev的安装目录,找到它)中修改77行:
playerPath=quick_cocos2dx_root+"/quick/player/win32"+"/player3.exe"
就能正确的运行player了。
)
拓展:
实际上QuickXDev提供的api还是比较有限的,比如在quick中比较常用的display中的函数基本在api代码补全和提示中看不到,这点也是让我有点伤心啊,难道真的没办法了么?还是有的!
参考了QuickXDev的api提示文件,我用python写了个,自动根据lua文件生成api提示的脚本,这个脚本的优势是能把当前文件夹下的所有.lua文件统一生成api提示,不仅仅是引擎的api,还有我们之前在程序中各个lua文件中的函数都能被智能识别,提示代码补全和提示功能,下面分享代码:
import os,sys,time
import Image
from kbtools import *
global mode
mode =[".lua",]
def Build_Snippet(filename):
if filename.endswith(mode[0]):
m_filename=os.path.split(filename)[-1]
m_exportName =m_filename.split(".")[0]+"_export"
if os.path.exists(os.path.abspath(m_exportName)) and os.path.isdir(os.path.abspath(m_exportName)):
pass
else:
os.mkdir(os.path.abspath(m_exportName))
print "export --- %s " %os.path.abspath(m_exportName)
with open(filename,"rb") as f:
for sub in f.readlines():
passContent ="".join(sub.split())
if passContent.startswith("function"):
CreateSnip(passContent,filename,os.path.abspath(m_exportName))
f.close()
# with open(filename)
else:
print "filename error"
def CreateSnip(string,fileVaule,dirname):
param_list =[]
left_index =string.find("(")
right_index =string.find(")")
index = string.find(".")
if string.find(".")==-1 and string.find(":")==-1:
return
elif string.find(":") >left_index or index > left_index:
return
elif string.find(".")!=-1 and string.find(":")!=-1:
pass
else:
index =max(index, string.find(":"))
function_name = string[index+1:left_index]
print "function_name %s" %function_name
newstr = string[left_index+1:right_index]
for substr in newstr.split(","):
param_list.append(substr)
#start create
file_content ="<snippet>\n"
file_content += "\t<content><![CDATA["+function_name+"("
par_num = 0
for each in param_list:
if par_num ==len(param_list)-1:
file_content +="${"+str(par_num)+":"+each+"})]]></content>\n"
else:
file_content +="${"+str(par_num)+":"+each+"},"
par_num =par_num +1
file_content +="\t<tabTrigger>"+string[index+1:len(string)]+"</tabTrigger>\n"
file_content +="\t<scope>source.lua</scope>\n"
file_content +="\t<description>"+fileVaule.split(".")[0].split("\\")[-1]+"</description>\n"
file_content +="</snippet>"
exportName =checkSNFile(os.path.split(fileVaule)[-1]+"_"+function_name)
# print " %s fileVaule %s" % (exportName,fileVaule)
with open(os.path.join(dirname,exportName),"w") as f:
f.write(file_content)
f.close()
def checkSNFile(filename):
newFile=filename
if os.path.exists(filename):
mfile =filename.split(".")[0]
newFile=mfile +"_extern"
newFile =newFile.replace(".","_")
newFile =newFile.replace(":","_")
newFile += ".sublime-snippet"
return checkSNFile(newFile)
else:
if filename.endswith(".sublime-snippet") == True:
newFile = filename.split(".")[0]
newFile = newFile.replace(".","_")
newFile = newFile.replace(":","_")
newFile +=".sublime-snippet"
else:
newFile = filename.replace(".","_")
newFile = filename.replace(":","_")
newFile += ".sublime-snippet"
return newFile
def setFlag(pmode):
global mode
mode[0] =pmode
if __name__ =="__main__":
if len(sys.argv) ==1:
FileSearch(".",os.getcwd())
for each in get_allFile_list():
Build_Snippet(each)
input("finish__________")
# Build_Snippet(os.path.join("a","display.lua"))
pass
效果如下:
这样我们就能轻松愉快的进行lua编码,麻麻再也不怕我写错代码了!
脚本文件下载:点击打开链接