Cocos2dx-lua functions

基于cocos2dx-lua 3.1 自己写的一些常用方法,现在分享出来,有不合理的地方还请指出来

–数组乱序
function XRLTools:arrayRandomSequence( array )
if not array or type(array) ~= “table” or #array < 2 then
print(“error array type !”)
return
end
local len = #array
for i=1,len do
math.randomseed(os.clock()+i)
local seed = math.random(1,len)
local seed2 = math.random(1,len)
local value = array[seed]
array[seed] = array[seed2]
array[seed2] = value
end
end

例如:local array = {1,2,3,4,5}
XRLTools:arrayRandomSequence( array )

–延时调用
function XRLTools:delaycall( time,callback )
if time <= 0 then
callback()
else
local delayAction = transition.sequence({
cc.DelayTime:create(time),
cc.CallFunc:create(callback),
})
local scene = display.getRunningScene()
scene:runAction(delayAction)
return delayAction
end
end

例如: XRLTools:delaycall( 1,function ( … )
print(“延时一秒打印”)
end)

–两区域是否重叠(可用于碰撞检测)
function XRLTools:rectIntersectsRect( A,B )
local rect1 = A:getBoundingBox()
local rect2 = B:getBoundingBox()
return cc.rectIntersectsRect(rect1, rect2)
end

–判断一个点是否落在一个node上面
function XRLTools:isTouchInNode(pt, node)
local s = node:getContentSize()
local rect
if s.width == 0 or s.height == 0 then
rect = cc.utils_:getCascadeBoundingBox(node)
else
pt = node:convertToNodeSpace(pt)
rect = cc.rect(0, 0, s.width, s.height)
end
if cc.rectContainsPoint(rect, pt) then
return true
end
return false
end


Leave a Reply

Your email address will not be published. Required fields are marked *