--绘制雷达动画
function MainScene:drawRadarAction()
if self.bgPen then
self.bgPen:clear()
else
math.newrandomseed()
local pen = cc.DrawNode:create()
pen:setRotation(30)
self:addChild(pen)
pen:setPosition(display.cx, display.cy - 200)
self.bgPen = pen
end
--边长
local sideLength = 200
local value = math.sqrt(3) / 2
--正六边形
local oirDots = {
cc.p(sideLength, 0),
cc.p(sideLength / 2, sideLength * value),
cc.p(-sideLength / 2, sideLength * value),
cc.p(-sideLength, 0),
cc.p(-sideLength / 2, -sideLength * value),
cc.p(sideLength / 2, -sideLength * value)
}
for i, v in ipairs(oirDots) do
self.bgPen:drawLine(cc.p(0, 0), v, cc.c4f(1, 1, 1, 0.5))
end
self.bgPen:drawPolygon(oirDots, #oirDots, cc.c4f(1, 1, 1, 0.5), 1, cc.c4f(1, 1, 1, 1))
if self.polygonPen then
self.polygonPen:clear()
else
local polygonPen = cc.DrawNode:create()
polygonPen:setRotation(30)
self:addChild(polygonPen)
polygonPen:setPosition(display.cx, display.cy - 200)
self.polygonPen = polygonPen
end
local oirPoints = {}
for i = 1, 6 do
oirPoints[i] = math.random(10, 100) / 100
end
table.walk(oirPoints,function(v, k)oirPoints[k] = v * sideLength end)
local dotOir = cc.p(0, 0)
local allTime = 20 --帧
local loopNum = 0
local function drawPoly(loopNum)
local newPoints = {}
for i = 1, 6 do
newPoints[i] = loopNum / allTime * oirPoints[i]
end
local newDots = {
[1] = cc.p(newPoints[1], 0),
[2] = cc.p(newPoints[2] / 2, newPoints[2] * value),
[3] = cc.p(-newPoints[3] / 2, newPoints[3] * value),
[4] = cc.p(-newPoints[4], 0),
[5] = cc.p(-newPoints[5] / 2, -newPoints[5] * value),
[6] = cc.p(newPoints[6] / 2, -newPoints[6] * value)
}
local triangles = {
[1] = {newDots[1], newDots[2], dotOir},
[2] = {newDots[2], newDots[3], dotOir},
[3] = {newDots[3], newDots[4], dotOir},
[4] = {newDots[4], newDots[5], dotOir},
[5] = {newDots[5], newDots[6], dotOir},
[6] = {newDots[6], newDots[1], dotOir}
}
self.polygonPen:clear()
for i, v in ipairs(triangles) do
self.polygonPen:drawSolidPoly(v, #v, cc.c4f(1, 0, 0, 0.5))
self.polygonPen:drawDot(newDots[i], 6, cc.c4f(1, 1, 1, 1))
end
self.polygonPen:drawPoly(newDots, #newDots, true, cc.c4f(1, 1, 1, 1))
end
schedule(self,function ()
loopNum = loopNum + 1
drawPoly(loopNum)
if loopNum >= allTime then
self:stopAllActions()
end
end,0)
end
因为2dx的绘制凹多边形有bug,所以将六边形分解为多个三角形进行绘制,其它多边形同理。