学习lua的一些好文章:http://www.jb51.net/List/List_245_1.htm<a target=_blank href="http://blog.csdn.net/q277055799/article/details/8463883" >http://blog.csdn.net/q277055799/article/details/8463883</a>==========下面是一些杂项=======print("os.time()="..os.time() .. "os.date()="..os.date()) -- os.time()=1434333054 os.date()=Mon Jun 15 09:50:54 2015格式:月/日/年 os.date("%x",os.time()) -- 06/16/15对table的一些常用 *** 作:连接函数:table.concat(table,sep,start,end) —table,分隔符,开始和结束位置插入函数:table.insert(table,pos,value) —table,位置,值移除函数:table.remove(table,pos)升序排序:table.sort(table,comp)移除函数:table.remove(table,pos) --删除table数组部分位于pos位置的元素. 其后的元素会被前移. pos默认为table长度,即从最后一个元素删起.============END========下面的笔记都是在公司的学习笔记。1.九月农场中的util/tools.lua。self.iconBlue:setVisible(data.rewardLv > 1)==============敏感字检测:function violateWords(str,mark)--print("搜索敏感词语 1.不通过 2.屏蔽")if mark == 1 thenfor _,v in pairs(illegal_words_config.illegal_words_config) do--print("yy " .. v.word .. " = " .. str)if string.find(str,v.word) then--包含敏感词汇不允许通过ui_tips_layer_t:ins():showcolorTips(localizable.include_banWords)return falseendendreturn trueelseif mark == 2 then--包含敏感词汇屏蔽for _,v in pairs(illegal_words_config.illegal_words_config) do--print(v.word .. " 包含敏感词汇屏蔽")str = string.gsub(str,v.word,"****")endreturn strendend用法:function changename(self)local name = self.farmname:getText()if violateWords(name,1) thenlocal buff = gamesvr.packChangePlayername(name)netmng.sendGsData(buff)endend=======打印整个table中的内容=======function printtable3Level( table,strname )print(strname,"===================")for k,v in pairs(table) doprint(" --",k,v)if type(v) == "table" thenfor m,n in pairs(v) doprint(" |-",m,n)if type(n) == "table" thenfor p,q in pairs(n) doprint(" |-",p,q)endendendendendprint("==========================")end用法:printtable3Level(self.tblSignInData_,"gggggg")=======求table长度=======function tableLength(t)if tableIsEmpty(t) thenreturn 0endlocal count = 0for _ in pairs(t) docount = count + 1endreturn countend用法:self.tblFansNew_ = {}tableLength(self.tblFansNew_)=======时间格式=======function formatTimeHHMMSS( second )local h = math.floor(second / 3600)local m = math.floor((second % 3600) / 60)local s = math.floor((second % 3600) % 60)if h > 0 thenif s > 0 thenreturn string.format("%d时%d分%d秒",h,s)elseif m > 0 thenreturn string.format("%d时%d分",m)elsereturn string.format("%d小时",h)endelseif m > 0 thenif s > 0 thenreturn string.format("%d分%d秒",s)elsereturn string.format("%d分钟",s)endelsereturn string.format("%d秒",s)endend用法:self.labelTime_:setString(formatTimeHHMMSS(remainTime))=======获取2点间的距离=======function getTwoPointdistance(px1,py1,px2,py2)local deltaX,deltaY = px1 - px2,py1 - py2return math.sqrt(deltaX * deltaX + deltaY * deltaY)end用法:local distance = getTwoPointdistance(px,py,pLast.x,pLast.y)=======时间日期=======—年、月、日local today = os.date("%Y%m%d")local data = tonumber(today,10) + 1print("today== "..today .. "data=="..data) -- today==20150615 data==20150616—时、分、秒local today = os.date("%H%M%s")print("today=="..today) -- today==094139=====四舍五入取整========function math.round(value) value = math.floor(value + 0.5) return valueend用法:score = math.round(score)======是否触摸到子对象=======function istouchChildren(self,_touchX,_touchY) local lp = self.node_:convertToNodeSpace(ccp(_touchX,_touchY)) local rect = self.body_:boundingBox() if rect:containsPoint(lp) then return true end if self.menu_:isVisible() then lp = self.menu_:convertToNodeSpace(ccp(_touchX,_touchY)) local items = self.menu_:getChildren() for i = 0,items:count() - 1 do local item = tolua.cast(items:objectAtIndex(i),"CcmenuItem") if item:isVisible() then rect = item:boundingBox() if rect:containsPoint(lp) then return true end end end end return falseend=====table复制========function depcopytable(st) local tab = {} for k,v in pairs(st or {}) do if type(v) ~= "table" then tab[k] = v else tab[k] = depcopytable(v) end end return tabend用法:function initDailyTask(self) print("初始化每日任务表") self.tblDailyTask_ = {} --printtable3Level(daily_task_config.daily_task_config," " .. self.level_) local cfg = depcopytable(daily_task_config.daily_task_config) for _,v in pairs(cfg) do if self.level_ >= v.open_lv then -- printtable3Level(v," " .. self.level_) table.insert(self.tblDailyTask_,v) end endend=============公告今日不再显示============勾选时:local curtime = math.ceil((os.time() + 8*3600) / 86400) * 86400 - 8*3600运行时:local dataStr = localstorage:localstorageGetItem("isNoShowNoticeTime") local curtime = tonumber(os.time()) local lasttime = dataStr if lasttime == nil or lasttime == "" then lasttime = 0 else lasttime = tonumber(dataStr) endif lasttime < curtime then print(“==显示公告==")end=============随机数============1.local random = math.random(1,#tips_config.tips_config)2.math.randomseed(os.time())print(math.random(3,4)) -- 输出不是3就是4============排序===========参考:http://www.jb51.net/article/55718.htmlocal test0 ={1,9,2,8,3,7,4,6} --table.sort(test0) --从小到大排序 table.sort(test0,function(a,b) return a>b end) —从大到小排序 for i,v in pairs(test0) do print("i==%d"..i.."v==%d"..v) end<pre name="code" >============比较两个时间点(os.date())之间的时间间隔值======= --[[比较两个时间,返回相差多少时间]] function timediff(long_time,short_time) local n_short_time,n_long_time,carry,diff = os.date('*t',short_time),os.date('*t',long_time),false,{} local colMax = {60,60,24,os.time{year=n_short_time.year,month=n_short_time.month+1,day=0}).day,12,0} n_long_time.hour = n_long_time.hour - (n_long_time.isdst and 1 or 0) + (n_short_time.isdst and 1 or 0) -- handle dst for i,v in ipairs({'sec','min','hour','day','month','year'}) do diff[v] = n_long_time[v] - n_short_time[v] + (carry and -1 or 0) carry = diff[v] < 0 if carry then diff[v] = diff[v] + colMax[i] end end return diff end local n_long_time = os.date(os.time{year=2014,month=6,day=10,hour=16,min=0,sec=0}); local n_short_time = os.date(os.time{year=2013,month=5,day=11,sec=0}); local t_time = timediff(n_long_time,n_short_time); local time_txt = string.format("%04d",t_time.year).."年"..string.format("%02d",t_time.month).."月"..string.format("%02d",t_time.day).."日 "..string.format("%02d",t_time.hour)..":"..string.format("%02d",t_time.min)..":"..string.format("%02d",t_time.sec); print(time_txt);总结
以上是内存溢出为你收集整理的cocos2dx-lua方法笔记全部内容,希望文章能够帮你解决cocos2dx-lua方法笔记所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)