本人Lua新手,写着玩玩。
新建一个模版project。
一、改动main.lua
require "Cocos2d"
require "Cocos2dConstants"
-- cclog
cclog = function(...)
print(string.format(...))
end
-- for CCLuaEngine traceback
function __G__TRACKBACK__(msg)
cclog("----------------------------------------")
cclog("LUA ERROR: " .. tostring(msg) .. "\n")
cclog(debug.traceback())
cclog("----------------------------------------")
return msg
end
local function main()
collectgarbage("collect")
-- avoid memory leak
collectgarbage("setpause", 100)
collectgarbage("setstepmul", 5000)
cc.Director:getInstance():getOpenGLView():setDesignResolutionSize(480, 320, 0)
cc.FileUtils:getInstance():addSearchPath("src");
cc.FileUtils:getInstance():addSearchPath("res");
--support debug
local targetPlatform = cc.Application:getInstance():getTargetPlatform()
if (cc.PLATFORM_OS_IPHONE == targetPlatform) or (cc.PLATFORM_OS_IPAD == targetPlatform) or
(cc.PLATFORM_OS_ANDROID == targetPlatform) or (cc.PLATFORM_OS_WINDOWS == targetPlatform) or
(cc.PLATFORM_OS_MAC == targetPlatform) then
cclog("result is ")
--require('debugger')()
end
-- run
local sceneGame = cc.Scene:create()
sceneGame:addChild(require("scripts.StartScene"))
if cc.Director:getInstance():getRunningScene() then
cc.Director:getInstance():replaceScene(sceneGame)
else
cc.Director:getInstance():runWithScene(sceneGame)
end
end
local status, msg = xpcall(main, __G__TRACKBACK__)
if not status then
error(msg)
end
二、新建一个目录scripts,当中新建一个StartScene.lua文件
local sceneStart = nil
visibleSize = cc.Director:getInstance():getVisibleSize()
-- create text
local function createText()
local alert = ccui.Text:create()
alert:setString("Hello World")
alert:setFontName("Marker Felt")
alert:setFontSize(30)
alert:setColor(cc.c3b(255, 255, 0))
alert:setPosition(cc.p(visibleSize.width / 2.0, visibleSize.height / 2.0 ))
return alert
end
-- create farm
local function createLayerFarm()
local layerFarm = cc.Layer:create()
-- add text
local spriteText = createText()
layerFarm:addChild(spriteText)
return layerFarm
end
if sceneStart == nil then
sceneStart = cc.Scene:create()
sceneStart:addChild(createLayerFarm())
end
return sceneStart