function GameScene:clone(object)
local lookup_table = {}
local function _copy(object)
if type(object) ~= "table" then
return object
elseif lookup_table[object] then
return lookup_table[object]
end
local new_table = {}
lookup_table[object] = new_table
for key, value in pairs(object) do
new_table[_copy(key)] = _copy(value)
end
return setmetatable(new_table, getmetatable(object))
end
return _copy(object)
end
--求阶乘
function GameScene:factorial(n)
if n== 0 then
return 1
else
return n * self:factorial(n-1)
end
end
--获取贝塞尔曲线上面的点
function GameScene:getBezierPos(posData,t)
local data = self:clone(posData)
local n = #data -1
local x = 0
local y = 0
for idx,pos in pairs(data) do
x = x + pos.x *(self:factorial(n)/(self:factorial(n-idx+1)*self:factorial(idx-1))) * math.pow(1-t,n-idx+1) * math.pow(t,idx-1)
y = y + pos.y *(self:factorial(n)/(self:factorial(n-idx+1)*self:factorial(idx-1))) * math.pow(1-t,n-idx+1) * math.pow(t,idx-1)
end
return cc.p(x,y)
end
function GameScene:test(layerFarm)
local drawNode = cc.DrawNode:create()
layerFarm:addChild(drawNode)
drawNode:setPosition(cc.p(5,100))
--控制点
local posData = {
cc.p(100,150),--起始点
cc.p(300,-80),--控制点可以有多个
cc.p(400,200),--终点
}
local startPos =
posData[1] --起始点位置
local distance = 20 --虚线的点之间的间隔
local radius = 3 --点的半径
local color = cc.c4f(1,0,0,1) --点的颜色
local time = 0
while time < 1 do
local pos = self:getBezierPos(posData,time)
local d = cc.pGetDistance(pos,startPos)
if d >= distance then
drawNode:drawDot(pos,radius,color)
startPos = pos
end
time = time + 0.001
end
--查看配置相关控制点
for idx,pos in pairs(posData)do
drawNode:drawDot(pos,5,color)
end
end