for i = 1, 10 do repeat if i == 5 then break end print(i) until true end lua里空格也算一个字符 类型:type(str) --在构造函数的最后的","是可选的,可以方便以后的扩展 local a = {[1]="red", [2]="green", [3]="blue",} table.concat 连接函数: local tbSpecial = {} for _, v in pairs(optSpecial) do if v == DN_PaiType.TongHuaShun then table.insert(tbSpecial,"同花顺") elseif v == DN_PaiType.ZhaDanNiu then table.insert(tbSpecial,"炸弹牛") elseif v == DN_PaiType.WuHuaNiu then table.insert(tbSpecial,"五花牛") end end local SpecialStr = table.concat(tbSpecial,",") Text_wangfa:setString(SpecialStr) bool值是不能直接转为number值的 self._mGou:SetVisible(nGou == 0 and false or true) @退出游戏:android: cc.Director:getInstance():endToLua(), ios: os.exit() @打印语句: print(dump(data)) @unpack: 用于返回 table 里的元素 local tbl = {"apple", "pear", "orange", "grape"} local a, b, c, d = unpack(tbl) @排序: table.sort(pushList, function(a, b) return a.sort < b.sort end) --table.sort默认是从小到大排序 --从大到小: local function sortFunc(a, b) return a > b end local tb = {10, 20, 30,50,5,9,8} table.sort(tb, sortFunc) function sortfun(a,b) 不能直接return true这样子 end table.sort(tbbbb, sortfun) @多条件排序: local function table_sort_Adornings(a, b) --蛋位序号排序 local result if a.isUsing >= 0 and b.isUsing >= 0 then --a,b已放置 if a.isUsing == b.isUsing then result = a.positionId < b.positionId else result = a.isUsing < b.isUsing end elseif a.isUsing < 0 and b.isUsing < 0 then --a,b未放置 local itemBaseA = _G.ItemTable():getItemById(a.adorningId); local itemBaseB = _G.ItemTable():getItemById(b.adorningId); local starA, starB = itemBaseA.startLv, itemBaseB.startLv result = starA > starB else result = a.isUsing > b.isUsing end return result end _G.table.sort(mAllDataTab, table_sort_Adornings) --按key值从小到大排序 local keytb = {x = 1, m = 2, y = 3, a = 4} local tempkey = {} local tbtempkey = {} for k,_ in pairs(keytb) do tempkey[#tempkey+1] = k end table.sort(tempkey) print(dump(tempkey)) @获取未来某时间点的时间戳: 犹如第二天9点通知玩家领取奖励之类的需求,要求获取第二天9点的时间戳。 local future_days = 2 local cur_timestamp = os.time() local one_hour_timestamp = 24*60*60 local temp_time = cur_timestamp + one_hour_timestamp * future_days local temp_date = os.date("*t", temp_time) return os.time({year=temp_date.year, month=temp_date.month, day=temp_date.day, hour=future_hour}) @判断Table是否为空: 使用Lua内置next函数: local a = {} if next(a) ~=nil then dosomething end @获取当前时间:年月日,时分秒,注意区分大小写: local nowHour = os.date("%Y/%m/%d", 656989) local nowHour2 = os.date("%H:%M:%S", 656989) %w:本地星期几 print(nowHour.." "..nowHour2) 方法二: local date = os.date("*t", time) local year = date.year local month = date.month local day = date.day local hour = date.hour local minute = date.min local second = date.sec --不足2位用0补 if string.len(date.month) < 2 then month = "0"..date.month end 方法三: local day = math.floor(nSurplusTime / 24 / 3600) local hour = math.floor(nSurplusTime / 3600) % 24 local min = math.floor((nSurplusTime % 3600) % 3600 / 60) local sec = math.floor((nSurplusTime % 3600) % 3600 % 60) @table赋值: local a = {} -- 创建一个table,并将它的引用存储在a a["x"] = 10 local b = a -- b与a引用同一个table print(b["x"]) --10 b["x"] = 20 print(a["x"]) --20 @table. remove :移除元素操作:-- 正确,从后往前遍历 local t = {1,2,3,3,5,3,6} for i=#t, 1, -1 do if t[i] == 3 then table.remove(t,i) end end @ Lua Json使用 local str = json.encode({a=50,b="ok",c={c1=8,c2="abc"},d={10,11},100}) print(str) local tb = json.decode(str) print(tb["b"]) print(tb["c"]["c2"]) @ 随机打乱顺序: function tigerMachine:getNumList(len) local rsList = {} for i = 1,len do table.insert(rsList,i) end local num,tmp for i = 1,len do num = math.random(1,len) tmp = rsList[i] rsList[i] = rsList[num] rsList[num] = tmp end return rsList end @代替continue的方法: end @条件判断:self.isreq and 300 or 0 :相当于:if self.isreq then 300 lese 0 index = index == 0 and 30 or index @lua自带的打印语句:dump(tttt) print(unpack(tongShaList)) @ 获取当前年月日时分秒:local date=os.date(“%Y-%m-%d %H:%M:%S”); local tm = os.date("*t") print(tm.year.."."..tm.month.."."..tm.day.." "..tm.hour..":"..tm.min..":"..tm.sec) --日期和时间 local DateTime = os.date("%Y-%m-%d", 22256) local Time = os.date("%H-%M-%S", 321) print("DateTime==="..DateTime.." Time=="..Time) ---DateTime===1970-01-01 Time==08-05-21 type(iValue) == "number" @setmetatable(table,metatable): 这个方法是用来设置元表的一个表。 getmetatable(table): 此方法用于获取表的元表。 @Lua之UnPack: unpack它接受一个数组(table)作为参数,并默认从下标1开始返回数组的所有元素 arrayData = {"a", "b", "c", "d", "e"}; print(unpack(arrayData)); -- abcde print(unpack(arrayData, 2));--bcde @table排序: local sortfunction = function(a1, b1) --从小到大排序 return a1.index < b1.index end table.sort(tb3, sortfunction) @去除table中的重复元素 function table_unique(t) local check = {}; local n = {}; for key , value in pairs(t) do if not check[value] then n[key] = value check[value] = value end end return n end @判断table是否为空:table_isEmpty(DateToInfo) function MJView:TableIsEmpty(table) return _G.next(table) == nil end --获取子控件: local clickItem = friendlist:getItem(_curIndex); local btnsuoqu = clickItem:findChildByName("btnsuoqu"); @string.match(): local str = "125;199" local num1, num2 = string.match(str,"(%d+);(%d+)") --125, 199 @数字变换: function numToFont(num) if num < 10000 then return num end local numStr = "" local wang = 10000 local yi = 100000000 if num >= wang and num < yi then local intNum = math.modf(num/wang) --取整数 local intMod = math.fmod(num, wang) -- 取余数 numStr = intNum.."万"..intMod elseif num >= yi then local intNumYi = math.modf(num/yi) local intModYi = math.fmod(num, yi) local intNumWang = math.modf(intModYi/wang) local intModWang = math.fmod(intModYi, wang) numStr = intNumYi.."亿"..intNumWang.."万"..intModWang end return numStr end print(numToFont(123456789)) @string.match用法: local strr = "11;22;33" local id, state, index = string.match(strr, "(%d+);(%d+);(%d+)") print("---id, state, index----", id, state, index) @-判断字符串是否由字母或者数字组成: G_String_Isalnum = function(str) if type(str) ~= "string" then return false end local len = string.len(str) for i = 1, len do local ch = string.sub(str, i, i) if not ((ch >= 'a' and ch <= 'z') or (ch >= 'A' and ch <= 'Z') or (ch >= '0' and ch <= '9')) then return false end end return true end 删除有序table中的值: for i=#tt, 1, -1 do table.remove(tt, i) end