代码来自cocos论坛用户pencik
let cmt = this.graphics.getComponent(cc.Graphics);
let startPos = cc.v2(300, 300);
let endPos =cc.v2(608, 987);
let line = endPos.sub(startPos);
let lineLenth = line.mag();
let unitLenth = 20;
let increment = line.normalize().mul(unitLenth);
let drawLine = true;
let pos = startPos.clone();
cmt.strokeColor = cc.Color.BLUE;
for(; lineLenth > unitLenth; lineLenth -= unitLenth){
if(drawLine){
cmt.moveTo(pos.x, pos.y);
pos.addSelf(increment);
cmt.lineTo(pos.x, pos.y);
cmt.stroke();
}else{
pos.addSelf(increment);
}
drawLine = !drawLine;
}
//最后一段
if(drawLine){
cmt.moveTo(pos.x, pos.y);
cmt.lineTo(endPos.x, endPos.y);
cmt.stroke();
}
开启抗锯齿(新建一个js文件,写入以下代码即可)
if(!CC_EDITOR){
cc.macro.ENABLE_WEBGL_ANTIALIAS = true;
}