前言:在开发cocos2dx的lua代码的时候,经常会打印log获取信息,不断开关游戏重新更新lua代码。这里分享一份文件,方便大家调试。
function printTT(content, ...)
local tab = 0
local out_list = {}
local function printk(value, key, tab)
if key == nil then
return
end
if type(key) ~= "number" then
key = tostring(key)
else
key = string.format("[%d]", key)
end
if type(value) == "table" then
if key ~= nil then
table.insert(out_list, tab .. key .. " =")
end
table.insert(out_list, tab .. "{")
for k, v in pairs(value) do
printk(v, k, tab .. "| ")
end
table.insert(out_list, tab .. "},")
else
local content
if type(value) == "nil" or value == "^&nil" then
value = "nil"
elseif type(value) == "string" then
value = string.format("\"%s\"", tostring(value))
else
value = tostring(value)
end
content = string.format("%s%s = %s,", tab, key, value)
table.insert(out_list, tostring(content))
end
end
local value = type(content) == "string" and string.format(content, ...) or content
local key = os.date("[\"%X\"]", os.time())
printk(value, key, "")
local out_str = table.concat(out_list, "\n")
print(out_str .. "\n")
-- local logFileName = os.date("print_tab_%Y_%m_%d.log", os.time())
local logFileName = "a.log"
local file = assert(io.open(logFileName, "a+"))
file:write(out_str .. "\n")
file:close()
end
function logd(...)
printt(...)
end
function printString(...)
local arg = {...}
local formatString = ""
for i = 1, #arg do
formatString = formatString .. "%s "
arg[i] = tostring(arg[i])
end
printTT(formatString, unpack(arg))
end
function logdw(content)
printTT(debug.traceback(content))
end
function logDump(value, desciption, nesting)
if type(nesting) ~= "number" then nesting = 3 end
local lookupTable = {}
local result = {}
local traceback = string.split(debug.traceback("", 2), "\n")
print("dump from: " .. string.trim(traceback[3]))
local function dump_(value, desciption, indent, nest, keylen)
desciption = desciption or ""
local spc = ""
if type(keylen) == "number" then
spc = string.rep(" ", keylen - string.len(dump_value_(desciption)))
end
if type(value) ~= "table" then
result[#result +1 ] = string.format("%s%s%s = %s", indent, dump_value_(desciption), spc, dump_value_(value))
elseif lookupTable[tostring(value)] then
result[#result +1 ] = string.format("%s%s%s = *REF*", indent, dump_value_(desciption), spc)
else
lookupTable[tostring(value)] = true
if nest > nesting then
result[#result +1 ] = string.format("%s%s = *MAX NESTING*", indent, dump_value_(desciption))
else
result[#result +1 ] = string.format("%s%s = {", indent, dump_value_(desciption))
local indent2 = indent.." "
local keys = {}
local keylen = 0
local values = {}
for k, v in pairs(value) do
keys[#keys + 1] = k
local vk = dump_value_(k)
local vkl = string.len(vk)
if vkl > keylen then keylen = vkl end
values[k] = v
end
table.sort(keys, function(a, b)
if type(a) == "number" and type(b) == "number" then
return a < b
else
return tostring(a) < tostring(b)
end
end)
for i, k in ipairs(keys) do
dump_(values[k], k, indent2, nest + 1, keylen)
end
result[#result +1] = string.format("%s}", indent)
end
end
end
dump_(value, desciption, "- ", 1)
local logFileName = "a.log"
local file = assert(io.open(logFileName, "a+"))
for i, line in ipairs(result) do
print(line)
file:write(line .. "\n")
end
file:close()
end
local function dump_value_(v)
if type(v) == "string" then
v = "\"" .. v .. "\""
end
return tostring(v)
end
function reload(file)
package.loaded[file] = nil
require(file)
end
function FightPanel:drawLine(mainNode, len, pos)
local drawNode = cc.DrawNode:create()
local r, g, b = "ff", "ff", "ff"
drawNode:drawSegment(cc.p(0, 0), cc.p(len, 0), 0.2,
ccc4f(tonumber(r, 16)/255, tonumber(g, 16)/255, tonumber(b, 16)/255, 1))
drawNode:setPosition(pos)
mainNode:addChild(drawNode, 3322)
return drawNode
end
function FightPanel:drawCube(mainNode, lenTab, pos)
local drawNode = cc.DrawNode:create()
local r, g, b = "ff", "ff", "ff"
drawNode:drawRect(cc.p(0, 0), cc.p(lenTab.w, 0),cc.p(lenTab.w, lenTab.h), cc.p(0, lenTab.h),
ccc4f(tonumber(r, 16)/255, tonumber(g, 16)/255, tonumber(b, 16)/255, 1))
drawNode:setPosition(pos)
mainNode:addChild(drawNode, 3333)
return drawNode
end
--屏幕上画网格
--drawScreenCube(self.fightScene.groundLayer, {h = 50, w = 30})
function FightPanel:drawScreenCube(mainNode, lenTab)
local size = cc.Director:getInstance():getWinSize()
local line = size.width / lenTab.w
local height = size.height / lenTab.h
for h = 0, height - 1 do
local y = lenTab.h * h
for w = 0, line - 1 do
local x = lenTab.w * w
self:drawCube(mainNode, lenTab, cc.p(x, y))
end
end
end
--检测3d模型碰撞
function intersects()
--add
local getNew3D = function(x, y)
local fileName = "tor.c3b"
local sprite = cc.Sprite3D:create(fileName)
sprite:setScale(0.3)
sprite:setRotation3D(cc.vec3(90,0,180))
sprite:setPosition3D(cc.vec3(x, y, 0))
local s = cc.Director:getInstance():getWinSize()
self.rootNode:addChild(sprite)
local aabb = sprite:getAABB()
return cc.OBB:new(aabb)
end
local obb1 = getNew3D(200, 200)
local obb2 = getNew3D(250, 200)
logd(obb1:intersects(obb2))
end
用法 :
1,在cocos2dx调用到lua的main.lua添加这些测试代码,即可全局调用
2, 打印数据量小的table
local tab = {1, 2, 3}
logd(tab)
3,打印数据量大的table
local bigTab = {...}
logDump(bigTab)
4,打印堆栈调用信息
logw("Stack")
5,(2和3步骤)输出的log都在根目录的docdir目录下,打开a.log查看log即可。
6,不关游戏,直接重载某份lua代码,比如要重载 fight.lua
(我的做法是在主界面的右下角做一个小小的按钮,点击按钮的时候调用reload函数,这样子想重载某些lua文件,点击这个按钮即可重载完成。)
reload("modules/fight/model/fight")
PS : 小小动作但是大大成效,这样子调试各种业务会省去大量的时间