我有一个节点Node ,它有很多个子节点,我用鼠标点击屏幕上的这个节点时,会获得鼠标事件的世界坐标系位置点,我用这个点,和Node的范围BoundingBoxToWorld 对照,以判定是否包含了这个鼠标点击点,但是结果试错的,为什么会这样呢?
代码如下所示,我绑定了一个点击事件。
this.stationNode.on(Input.EventType.TOUCH_START,this.onStationTouchStart,this);
在这个点击事件里,我遍历这个节点Node的所有子节点的世界坐标范围,用 contains 来判断是否包含,但是很不幸,判断的是结果是错误的。判断的范围和鼠标点击的点不是对应的,这是bug吗?
onStationTouchStart(event:EventTouch) {
let point = event.getLocation();//.add(v2(-this.stationWidth/2,-this.stationWidth/2));
const target = event.target as Node;
//console.log('点击位置getLocation:',event.getLocation());
for(let n of this.stationNode.children){
let lbl:Label = n.getChildByName("Label").getComponent(Label);
if(n.getComponent(UITransform).getBoundingBoxToWorld().contains(point)){
let lbl:Label = n.getChildByName("Label").getComponent(Label);
console.log('你点击了',lbl.string);
}
}
}