--打印table
function print_r(root, desc)
if not Config.Debug then
return
end
local print = print
local tconcat = table.concat
local tinsert = table.insert
local srep = string.rep
local type = type
local pairs = pairs
local tostring = tostring
local next = next
local cache = { [root] = "." }
local function _dump(t,space,name)
local temp = {}
for k,v in pairs(t) do
local key = tostring(k)
if cache[v] then
tinsert(temp,"+" .. key .. " {" .. cache[v].."}")
elseif type(v) == "table" then
local new_key = name .. "." .. key
cache[v] = new_key
tinsert(temp,"+" .. key .. _dump(v,space .. (next(t,k) and "|" or " " ).. srep(" ",#key),new_key))
else
tinsert(temp,"+" .. key .. " [" .. tostring(v).."]")
end
end
return tconcat(temp,"\n"..space)
end
print((desc or "") .. " print_r\n" .. _dump(root, "",""))
end
--深度拷贝,改变拷贝的table,不会改变原来table的值,
function deepCopy(ori_tab)
if (type(ori_tab) ~= "table") then
return nil;
end
local new_tab = {};
for i,v in pairs(ori_tab) do
local vtyp = type(v);
if (vtyp == "table") then
new_tab[i] = deepCopy(v);
elseif (vtyp == "thread") then
new_tab[i] = v;
elseif (vtyp == "userdata") then
new_tab[i] = v;
else
new_tab[i] = v;
end
end
return new_tab;
end
--字符串切割
function split(str, delim, maxNb)
-- Eliminate bad cases...
if string.find(str, delim) == nil then
return { str }
end
if maxNb == nil or maxNb < 1 then
maxNb = 0 -- No limit
end
local result = {}
local pat = "(.-)" .. delim .. "()"
local nb = 0
local lastPos
for part, pos in string.gmatch(str, pat) do
nb = nb + 1
result[nb] = part
lastPos = pos
if nb == maxNb then break end
end
-- Handle the last field
if nb ~= maxNb then
result[nb + 1] = string.sub(str, lastPos)
end
return result
end
local str = "1,2,3,4"
local newStr = split(str, ",")
print(unpack(newStr)) --1 2 3 4
-- 每个字后面增加一个换行,解决文字竖着写的问题
function stringInsert(str, insertStr)
local len = #str
local left = len
local cnt = 0
local arr={0,0xc0,0xe0,0xf0,0xf8,0xfc}
local indx = -left
local newstr = ""
while left ~= 0 do
local tmp=string.byte(str,-left)
local i=#arr
while arr[i] do
if tmp>=arr[i] then
left=left-i
break
end
i=i-1
end
local substr = string.sub(str, indx, -left-1)
if left ~= 0 then
newstr = newstr .. substr .. insertStr
else
newstr = newstr .. substr
end
indx = -left
cnt=cnt+1
end
return newstr
end
local str = "大家好"
print(stringInsert(str, '\n'))
-- 保留小数点后n位
function getPreciseDecimal(nNum, n)
if type(nNum) ~= "number" then
return nNum
end
n = n or 0
n = math.floor(n)
if n < 0 then
n = 0
end
local nDecimal = 10 ^ n
local nTemp = math.floor(nNum * nDecimal)
local nRet = nTemp / nDecimal
return nRet
end
--判断移动方向
self:addEventListener(Event.TouchEvent, onTouchLayer, self)
function onTouchLayer(self,event,target)
if Event.Touch_began == event.etype then
self._oriPos = event.p
elseif Event.Touch_moved == event.etype then
if self._oriPos then
local dis = getSquareDistance(self._oriPos, event.p) --有效移动距离
if dis >= 300 then
local angle = getAngleByPos(self._oriPos, event.p)
local dir = "左"
if angle > -45 and angle <= 45 then
dir = "右"
elseif angle > 45 and angle <= 135 then
dir = "上"
elseif angle > -135 and angle <= -45 then
dir = "下"
end
self._oriPos = nil
return
end
end
elseif Event.Touch_ended == event.etype or Event.Touch_out == event.etype then
self._oriPos = nil
end
end
function getSquareDistance(startPos, endPos)
local dx = startPos.x - endPos.x
local dy = startPos.y - endPos.y
return (dx * dx + dy * dy)
end
function getAngleByPos(p1, p2)
local p = {}
p.x = p2.x - p1.x
p.y = p2.y - p1.y
local r = math.atan2(p.y,p.x)*180/math.pi
-- print("夹角[-180 - 180]:",r)
return r
end
function onClickClose(self)
UIManager.closeUI(self._name)
end
--对string进行分隔符换行操作
local str = "同时我们建议|您前往新区|获得更佳的|游戏体验,在新区建号|可联系客服领取"
local pos = string.find(str, "|", 1)
local len = string.len(str)
local index = 0
local currPos = 1
local strTB = {}
if pos == nil then
table.insert(strTB, str)
else
while (pos and pos <= len) do
index = index + 1
local currStr = string.sub(str, currPos, pos-1)
currStr = string.format("%s",currStr)
table.insert(strTB, currStr)
currPos = pos + 1
pos = string.find(str, "|" , currPos+1)
if pos == nil then
local last = string.sub(str,currPos,len)
last = string.format("%s",last)
index = index + 1
table.insert(strTB, last)
end
end
end
Lua比较好的方法函数
于 2022-02-24 11:54:58 首次发布