cocos js 模拟闪电特效

参考:https://krazydad.com/bestiary/bestiary_lightning.html

直接上核心代码:

var drawLighting = function (x1, y1, x2, y2, displace) {
    if (displace < curDetail) {
        drawNode.drawSegment(cc.p(x1, y1), cc.p(x2, y2), 5, cc.color(255, 255, 255));
    } else {
        var mid_x = (x1 + x2) / 2;
        var mid_y = (y1 + y2) / 2;
        mid_x += (Math.random() - 0.5) * displace;
        mid_y += (Math.random() - 0.5) * displace;
        drawLighting(x1, y1, mid_x, mid_y, displace / 2);
        drawLighting(x2, y2, mid_x, mid_y, displace / 2);
    }
};

具体调用:

var start = cc.p(100,300);
var end = cc.p(800,300);
var displace = 200;

//每帧调用一次
update: function (dt) {
 drawLighting(start.x, start.y, end.x, end.y, displace);
}

效果图:

cocos js 模拟闪电特效_第1张图片

你可能感兴趣的:(cocos)