前面已经了解了 quick-lua 脚本加载流程 ,下面看看如何创建项目。
创建项目脚本在quick根目录下的bin目录中。create_project.bat和create_project.sh。根据不同的系统使用不同的脚本。创建项目的语法为 create_projet.sh com.xxx.yyy。新项目的路径为shell当前路径。所以,一般在根目录下创建一个projects目录,然后在该目录中执行脚本../bin/create_projet.sh com.xxx.yyy
新建项目包含些神马东西呢?
res用于存放资源,相当于cocos2dx的resource目录,用于存放资源。scripts用于存放脚本。sources相当于cocos2dx的classes目录,用于存放c++文件。
新项目的main仅仅执行了require("app.MyApp").new():run()。然后myApp加载MainScene。下面看看这几个文件的内容。
- function __G__TRACKBACK__(errorMessage)
- print("----------------------------------------")
- print("LUA ERROR: " .. tostring(errorMessage) .. "\n")
- print(debug.traceback("", 2))
- print("----------------------------------------")
- end
- <pre name="code" class="html">require("config") -- 加载config
- require("framework.init") -- 加载quick提供的简化开发流程的lua脚本
- require("framework.shortcodes") -- quick提供了一组名字较短的函数,封装了cocos的冗长名称。简化开发
- require("framework.cc.init") -- 加载ui,事件,app等脚本
-
- local MyApp = class("MyApp", cc.mvc.AppBase)
-
- function MyApp:ctor()
- MyApp.super.ctor(self)
- end
-
- function MyApp:run()
- CCFileUtils:sharedFileUtils():addSearchPath("res/")
- self:enterScene("MainScene")
- end
-
- return MyApp</pre><br><br>
- function MainScene:ctor()
- ui.newTTFLabel({text = "Hello, World", size = 64, align = ui.TEXT_ALIGN_CENTER})
- :pos(display.cx, display.cy)
- :addTo(self)
- end
-
- function MainScene:onEnter()
- if device.platform == "android" then
- -- avoid unmeant back
- self:performWithDelay(function()
- -- keypad layer, for android
- local layer = display.newLayer()
- layer:addKeypadEventListener(function(event)
- if event == "back" then app.exit() end
- end)
- self:addChild(layer)
-
- layer:setKeypadEnabled(true)
- end, 0.5)
- end
- end
-
- function MainScene:onExit()
- end
-
- return MainScene