PIXI8穿透点击事件封装

文档创建者:admin
浏览次数:33
最后更新:2025-06-20
  1.   function 事件穿透(e, currentObject) {
  2.     // 获取全局点击坐标
  3.     const globalX = e.global.x;
  4.     const globalY = e.global.y;
  5.    
  6.     // 找出所有在点击位置下的交互对象
  7.     // 遍历舞台的所有子元素(跳过当前对象自身)
  8.     app.stage.children.forEach(child => {
  9.       // 跳过当前对象和非交互对象
  10.       if (child === currentObject || !child.interactive) return;
  11.       
  12.       // 检查点击是否在这个对象的区域内
  13.       // 注意:这里假设所有对象都是矩形,复杂形状可能需要更复杂的碰撞检测
  14.       if (child.getBounds) {
  15.         const bounds = child.getBounds();
  16.         if (globalX >= bounds.x && globalX <= bounds.x + bounds.width &&
  17.             globalY >= bounds.y && globalY <= bounds.y + bounds.height) {
  18.           // 手动触发这个对象的事件
  19.           child.emit('pointerdown', e);
  20.         }
  21.       }
  22.     });
  23.   }
复制代码

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则