具體在quick-cocos2d-x 框架:framework->function.lua 文件中
--[[--
檢查并嘗試轉(zhuǎn)換為數(shù)值赫编,如果無法轉(zhuǎn)換則返回 0
@param mixed value 要檢查的值
@param [integer base] 進制颓帝,默認為十進制
@return number
]]
function checknumber(value, base)
? ? ? ? return tonumber(value, base) or 0
end
--[[--
檢查并嘗試轉(zhuǎn)換為整數(shù)耗溜,如果無法轉(zhuǎn)換則返回 0
@param mixed value 要檢查的值
@return integer
]]
function checkint(value)
? ? ? ? ? ?return math.round(checknumber(value))
end
--[[--
檢查并嘗試轉(zhuǎn)換為布爾值灭将,除了 nil 和 false,其他任何值都會返回 true
@param mixed value 要檢查的值
@return boolean
]]
function checkbool(value)
? ? ? ? return (value ~= nil and value ~= false)
end
--[[--
檢查值是否是一個表格茫藏,如果不是則返回一個空表格
@param mixed value 要檢查的值
@return table
]]
function checktable(value)
? ? ? ? ?if type(value) ~= "table" then value = {} end
? ? ? ? ? return value
end
--[[--
如果表格中指定 key 的值為 nil矛物,或者輸入值不是表格,返回 false路翻,否則返回 true
@param table hashtable 要檢查的表格
@param mixed key 要檢查的鍵名
@return boolean
]]
function isset(hashtable, key)
? ? ? local t = type(hashtable)
? ? ?return (t == "table" or t == "userdata") and hashtable[key] ~= nil
end
-- 根據(jù)系統(tǒng)時間初始化隨機數(shù)種子狈癞,讓后續(xù)的 math.random() 返回更隨機的值
-- @function [parent=#math] newrandomseed
-- end --
function math.newrandomseed()
? ? ? ? local ok, socket = pcall(function()
? ? ? ? return require("socket")
? ? ? ? ?end)
? ? ? ? if ok then
? ? ? ? ? ? ?-- 如果集成了 socket 模塊,則使用 socket.gettime() 獲取隨機數(shù)種子
? ? ? ? ?math.randomseed(socket.gettime() * 1000)
? ? ? ? else
? ? ? ? ?math.randomseed(os.time())
? ? ? ? ?end
? ? ? ? ?math.random()
? ? ? ? ?math.random()
? ? ? ? ?math.random()
? ? ? ? math.random()
end
--------------------------------
-- 對數(shù)值進行四舍五入茂契,如果不是數(shù)值則返回 0
-- @function [parent=#math] round
-- @param number value 輸入值
-- @return number#number
-- end --
function math.round(value)
? ? ? ?value = checknumber(value)
? ? ? ?return math.floor(value + 0.5)
end