加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2dx-lua方法笔记

发布时间:2020-12-14 16:46:48 所属栏目:百科 来源:网络整理
导读:学习lua的一些好文章:http://www.jb51.net/list/list_245_1.htma 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/
学习lua的一些好文章:
http://www.aspzz.cn/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 then
for _,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 false
end
end

return true
elseif mark == 2 then
--包含敏感词汇屏蔽
for _,v in pairs(illegal_words_config.illegal_words_config) do
--print(v.word .. " 包含敏感词汇屏蔽")
str = string.gsub(str,v.word,"****")
end

return str
end
end
用法:
function changeName(self)
local name = self.farmname:getText()

if violateWords(name,1) then
local buff = gamesvr.packChangePlayerName(name)
netmng.sendGsData(buff)
end
end
=======打印整个table中的内容=======
function printTable3Level( table,strName )
print(strName,"===================")
for k,v in pairs(table) do
print(" --",k,v)
if type(v) == "table" then
for m,n in pairs(v) do
print(" |-",m,n)
if type(n) == "table" then
for p,q in pairs(n) do
print(" |-",p,q)

end
end
end
end
end
print("==========================")
end
用法:
printTable3Level(self.tblSignInData_,"gggggg")
=======求table长度=======
function tableLength(t)
if tableIsEmpty(t) then
return 0
end

local count = 0
for _ in pairs(t) do
count = count + 1
end
return count
end
用法:
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 then
if s > 0 then
return string.format("%d时%d分%d秒",h,s)
elseif m > 0 then
return string.format("%d时%d分",m)
else
return string.format("%d小时",h)
end
elseif m > 0 then
if s > 0 then
return string.format("%d分%d秒",s)
else
return string.format("%d分钟",s)
end
else
return string.format("%d秒",s)
end
end
用法:
self.labelTime_:setString(formatTimeHHMMSS(remainTime))
=======获取2点间的距离=======
function getTwoPointDistance(px1,py1,px2,py2)
local deltaX,deltaY = px1 - px2,py1 - py2
return 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) + 1
print("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 value
end
用法: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 false
end
=====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 tab
end
用法:
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
     end
end
=============公告今日不再显示============
勾选时: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)
        end
if 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.aspzz.cn/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.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); 

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读