CocosCreator-【射线】笔记
2018-11-06 本文已影响14人
伏波Rinnsio1xy
1.必须在onLoad开启物理引擎start函数开启也不能使用射线
Cococs Creator 2.0.2版本
具体原因,暂不知道为什么,这个点有点坑。不开启有2个问题:
第一个是射线不能正常工作,
第二个问题是,启动会报错,PhysicsManager的init函数抛出错误。
2.使用方式,需要转化为世界坐标,具体转化下面例子
3.例子:
开启物理引擎:
onLoad: function () {
cc.director.getPhysicsManager().enabled = true;
},
射线例子:
drawRayLine: function(lightType) {
var pW1 = this.btns[lightType].convertToWorldSpaceAR(cc.v2(0, 0))
var pW2 = cc.v2(pW1.x, pW1.y + 3000)
var p1 = this.ctx.node.convertToNodeSpaceAR(pW1)
var p2 = this.ctx.node.convertToNodeSpaceAR(pW2)
var results = this.phyManager.rayCast(pW1, pW2, this.rayCastType);
var rayLocalPoint = p2
if (this.rayCastType === cc.RayCastType.Closest) {
if (results[0]) {
var rayNode = results[0].collider.node
cc.log("ray cast node", rayNode.name)
rayLocalPoint = this.ctx.node.convertToNodeSpaceAR(results[0].point)
rayLocalPoint = cc.v2(p1.x, rayLocalPoint.y)
if (rayNode.group == "LightingEmitter") {
var rayLightType = rayNode.getComponent("LightingEmitterType").lightType
this.btns[rayLightType].addColor[lightType] = true
cc.log("LightingEmitterType", rayLightType)
}
}
}
this.btns[lightType].rayLineStartPoint = p1
this.btns[lightType].rayLineEndPoint = rayLocalPoint
},