cocos2dx-lua 复刻王者荣耀雷达图效果

--绘制雷达动画
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,所以将六边形分解为多个三角形进行绘制,其它多边形同理。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值