86 lines
2.2 KiB
TypeScript
86 lines
2.2 KiB
TypeScript
|
|
|
|
const { ccclass, property } = cc._decorator;
|
|
|
|
@ccclass
|
|
export default class LineLogic extends cc.Component {
|
|
|
|
points: any = [];
|
|
|
|
eachLineWidth: number = 20
|
|
|
|
prePoint: cc.Vec2;
|
|
preWorldPoint: cc.Vec2;
|
|
|
|
@property(cc.Node)
|
|
dashed: cc.Node = null;
|
|
|
|
dashedDraw: cc.Graphics;
|
|
|
|
draw: cc.Graphics
|
|
|
|
onLoad() {
|
|
this.dashedDraw = this.dashed.getComponent(cc.Graphics);
|
|
this.draw = this.getComponent(cc.Graphics)
|
|
console.log('加载完毕')
|
|
}
|
|
|
|
start() {
|
|
|
|
}
|
|
onTouchStart(e) {
|
|
console.log('测试')
|
|
var pos = e.getLocation();
|
|
this.preWorldPoint = pos;
|
|
|
|
pos = this.node.convertToNodeSpaceAR(pos)
|
|
this.prePoint = pos;
|
|
|
|
this.points.push(cc.v2(pos.x, pos.y))
|
|
this.draw.clear()
|
|
this.draw.moveTo(pos.x, pos.y)
|
|
|
|
return true
|
|
}
|
|
onTouchMove(e) {
|
|
var pos = e.getLocation();
|
|
var nowWorldPoint = pos;
|
|
pos = this.node.convertToNodeSpaceAR(pos)
|
|
|
|
var disX = pos.x - this.prePoint.x
|
|
var disY = pos.y - this.prePoint.y
|
|
var dis = Math.sqrt(disX * disX + disY * disY)
|
|
if (dis >= this.eachLineWidth) {
|
|
// 射线检测
|
|
let result = cc.director.getPhysicsManager().rayCast(this.preWorldPoint, nowWorldPoint, cc.RayCastType.All);
|
|
if (result.length <= 0) {
|
|
this.points.push(cc.v2(pos.x, pos.y))
|
|
this.draw.lineTo(pos.x, pos.y)
|
|
this.draw.stroke()
|
|
this.prePoint = pos;
|
|
this.preWorldPoint = nowWorldPoint;
|
|
this.dashedDraw.clear()
|
|
} else {
|
|
console.log('aaaaaa')
|
|
this.dashedDraw.clear();
|
|
this.dashedDraw.moveTo(this.prePoint.x, this.prePoint.y);
|
|
this.dashedDraw.lineTo(pos.x, pos.y);
|
|
this.dashedDraw.stroke();
|
|
}
|
|
}
|
|
}
|
|
onTouchEnd(e) {
|
|
|
|
this.dashedDraw.clear();
|
|
var len = this.points.length
|
|
this.addComponent(cc.RigidBody)
|
|
var collider: cc.PhysicsPolygonCollider = this.addComponent('MyPhysicsCollider')
|
|
collider.points = this.points
|
|
collider.density = 120
|
|
collider.friction = 0.99
|
|
|
|
collider.apply()
|
|
}
|
|
|
|
}
|