cocos2dx-lua方法笔记

来源:互联网 发布:sam 软件 编辑:程序博客网 时间:2024/05/21 09:39
学习lua的一些好文章:http://www.jb51.net/list/list_245_1.htm<a target=_blank href="http://blog.csdn.net/q277055799/article/details/8463883" style="font-family:'Helvetica Neue';font-size:14px;">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, m, s)elseif m > 0 thenreturn string.format("%d时%d分", h, m)elsereturn string.format("%d小时", h)endelseif m > 0 thenif s > 0 thenreturn string.format("%d分%d秒", m, s)elsereturn string.format("%d分钟", m, 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.htm local 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" class="cpp">============比较两个时间点(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.date('*t',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,hour=16,min=0,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); 


0 0