FixShowBox.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. cc.Class({
  2. extends: cc.Component,
  3. /* --------------------------------segmentation-------------------------------- */
  4. onLoad() {
  5. this.content = this.node.getChildByName("view").getChildByName("content");
  6. // ------------------事件监听
  7. this.node.on("scrolling", this._event_update_opacity, this);
  8. this.content.on(cc.Node.EventType.CHILD_REMOVED, this._event_update_opacity, this);
  9. this.content.on(cc.Node.EventType.CHILD_REORDER, this._event_update_opacity, this);
  10. },
  11. /* ***************功能函数*************** */
  12. /**获取在世界坐标系下的节点包围盒(不包含自身激活的子节点范围) */
  13. _get_bounding_box_to_world(node_o_) {
  14. let w_n = node_o_._contentSize.width;
  15. let h_n = node_o_._contentSize.height;
  16. let rect_o = cc.rect(
  17. -node_o_._anchorPoint.x * w_n,
  18. -node_o_._anchorPoint.y * h_n,
  19. w_n,
  20. h_n
  21. );
  22. node_o_._calculWorldMatrix();
  23. rect_o.transformMat4(rect_o, node_o_._worldMatrix);
  24. return rect_o;
  25. },
  26. /**检测碰撞 */
  27. _check_collision(node_o_) {
  28. let rect1_o = this._get_bounding_box_to_world(this.content.parent);
  29. let rect2_o = this._get_bounding_box_to_world(node_o_);
  30. // ------------------保险范围
  31. rect1_o.width += rect1_o.width * 0.5;
  32. rect1_o.height += rect1_o.height * 0.5;
  33. rect1_o.x -= rect1_o.width * 0.25;
  34. rect1_o.y -= rect1_o.height * 0.25;
  35. return rect1_o.intersects(rect2_o);
  36. },
  37. /* ***************自定义事件*************** */
  38. _event_update_opacity() {
  39. this.content.children.forEach(v1_o => {
  40. v1_o.opacity = this._check_collision(v1_o) ? 255 : 0;
  41. });
  42. },
  43. })