Flash AS3 让<鼠标点击>穿透上层透明区域,下层对象触发<鼠标点击>事件

问题描述:
1. 在Stage上放置2个Sprite, A 和 B, 它们中心区域和边界是不透明的,而边缘区域是透明的

2. A 在 B 的上层, A和B之间有重叠区域,如下图
  ( * 为 A 的中心区域, . 为 B 的中心区域, 空白处为透明区域 , X 为鼠标点击区域 )
   
3. 点击X位置时,如果让B来响应事件

  A-----------A
  | |
  | ****** |
  | ******---+------B
  | ****** | |
  | | ...|... |
  | | .X.|... |  
  | | ...|... |
  A----+------A... |
  | ....... |
  | |
  B-------------B

--------------------------------------------------------
                             解决方案
--------------------------------------------------------
使用InteractivePNG工具

InteractivePNG使PNG图像中的透明像素不再响应鼠标交互。
所涉及到的类文件
com/ibio8/media/images/MCInteractivePNG.as
http://www.mosessupposes.com/utilities/
概述

此类主要功能是将 PNG 图片的透明区域处理为没有鼠标事件的区域。以方便对包含透明位图的 MC 进行鼠标事件、检测碰撞等操作。
使用方法

mybtn1.addEventListener(MouseEvent.MOUSE_OVER, mouseHandler1);
mybtn1.addEventListener(MouseEvent.MOUSE_OUT, mouseHandler1);
function mouseHandler1(evt:Event):void {
if (evt.type=="mouseOver") {
bg1.gotoAndStop(2);
} else {
bg1.gotoAndStop(1);
}
}

mybtn2.addEventListener(MouseEvent.MOUSE_OVER, mouseHandler2);
mybtn2.addEventListener(MouseEvent.MOUSE_OUT, mouseHandler2);
function mouseHandler2(evt:Event):void {
if (evt.type=="mouseOver") {
bg2.gotoAndStop(2);
} else {
bg2.gotoAndStop(1);
}
}
重点剖析
实例:
http://www.mosessupposes.com/utilities/InteractivePNG_demo.html

使用方法:下载demo后 需和 工具类 包放同一目录下方可使用。

猜你喜欢

转载自ch-kexin.iteye.com/blog/2197751