as3 图片透明区域不接受鼠标事件的工具类

转自 http://blog.sina.com.cn/s/blog_6919c12201019o44.html

一牛人写的工具类,使透明图片转化为原件,原件继承了该类,就能使透明区域不接受鼠标事件,

package com.mosesSupposes.bitmap {
 import flash.display.Bitmap;
 import flash.display.BitmapData;
 import flash.display.MovieClip;
 import flash.display.Sprite;
 import flash.events.Event;
 import flash.events.MouseEvent;
 import flash.geom.Matrix;
 import flash.geom.Point;
 import flash.geom.Rectangle; 
 
 
 public class InteractivePNG extends MovieClip {
  
  // -== Public Properties ==-
  
  
  public function get interactivePngActive() : Boolean {
   return _interactivePngActive;
  }
  
  
  public function get alphaTolerance() : uint {
   return _threshold;
  }
  public function set alphaTolerance(value : uint) : void {
   _threshold = Math.min(255, value);
  }
  
  // Excluded from documentation for simplicity, a note is provided under disableInteractivePNG.
  
  override public function set hitArea(value : Sprite) : void {
   if (value!=null && super.hitArea==null) {
    disableInteractivePNG();
   }
   else if (value==null && super.hitArea!=null) {
    enableInteractivePNG();
   }
   super.hitArea = value;
  }
  
  // Excluded from documentation for simplicity, a note is provided under disableInteractivePNG.
  
  override public function set mouseEnabled(enabled : Boolean) : void {
   if (isNaN(_buttonModeCache)==false) { // indicates that mouse has entered clip bounds.
    disableInteractivePNG();
   }
   super.mouseEnabled = enabled;
  }
  
  // -== Private Properties ==-
  
  
  protected var _threshold : uint = 128;
  
  protected var _transparentMode : Boolean = false;
  
  protected var _interactivePngActive : Boolean = false;
  
  protected var _bitmapHit : Boolean = false;
  
  protected var _basePoint : Point;
  
  protected var _mousePoint : Point;
  
  protected var _bitmapForHitDetection : Bitmap;
  
  protected var _buttonModeCache : Number = NaN;
  
  // -== Public Methods ==-
  
  
  public function InteractivePNG():void {
   super();
   _basePoint = new Point();
   _mousePoint = new Point();
   enableInteractivePNG();
  }
  
  
  public function drawBitmapHitArea(event:Event=null) : void
  {
   var isRedraw:Boolean = (_bitmapForHitDetection!=null);
   if (isRedraw) {
    try { removeChild(_bitmapForHitDetection); }catch(e:Error) { }
   }
   var bounds:Rectangle = getBounds(this);
   var left:Number = bounds.left;
   var top:Number = bounds.top;
   var b:BitmapData = new BitmapData(bounds.width, bounds.height, true, 0);
   _bitmapForHitDetection = new Bitmap(b);
   _bitmapForHitDetection.name = "interactivePngHitMap"; // (So that it is not a mystery if the displaylist is being inspected!)
   _bitmapForHitDetection.visible = false;
   var mx:Matrix = new Matrix();
   mx.translate(-left, -top);
   b.draw(this, mx);
   addChildAt(_bitmapForHitDetection, 0);
   _bitmapForHitDetection.x = left;
   _bitmapForHitDetection.y = top;
  }
  
  
  public function disableInteractivePNG(): void {
   deactivateMouseTrap();
   removeEventListener(Event.ENTER_FRAME, trackMouseWhileInBounds);
   try { removeChild(_bitmapForHitDetection); }catch(e:Error) { }
   _bitmapForHitDetection==null;
   super.mouseEnabled = true;
   _transparentMode = false;
   setButtonModeCache(true);
   _bitmapHit = false;
   _interactivePngActive = false;
  }
  
  
  public function enableInteractivePNG(): void {
   disableInteractivePNG();
   if (hitArea!=null)
    return;
   activateMouseTrap();
   _interactivePngActive = true;
  }
   
  // -== Private Methods ==-
  
  
  protected function activateMouseTrap() : void {
   addEventListener(MouseEvent.ROLL_OVER, captureMouseEvent, false, 10000, true); //useCapture=true, priority=high, weakRef=true
   addEventListener(MouseEvent.MOUSE_OVER, captureMouseEvent, false, 10000, true);
   addEventListener(MouseEvent.ROLL_OUT, captureMouseEvent, false, 10000, true); 
   addEventListener(MouseEvent.MOUSE_OUT, captureMouseEvent, false, 10000, true);
   addEventListener(MouseEvent.MOUSE_MOVE, captureMouseEvent, false, 10000, true);
  }
  
  
  protected function deactivateMouseTrap() : void {
   removeEventListener(MouseEvent.ROLL_OVER, captureMouseEvent);
   removeEventListener(MouseEvent.MOUSE_OVER, captureMouseEvent);
   removeEventListener(MouseEvent.ROLL_OUT, captureMouseEvent); 
   removeEventListener(MouseEvent.MOUSE_OUT, captureMouseEvent);
   removeEventListener(MouseEvent.MOUSE_MOVE, captureMouseEvent);
  }
  
  
  protected function captureMouseEvent(event : Event) : void
  {
   if (!_transparentMode) {
    if (event.type==MouseEvent.MOUSE_OVER || event.type==MouseEvent.ROLL_OVER) {
     // The buttonMode state is cached then disabled to avoid a cursor flicker
     // at the movieclip bounds. Reenabled when bitmap is hit.
     setButtonModeCache();
     _transparentMode = true;
     super.mouseEnabled = false;
     addEventListener(Event.ENTER_FRAME, trackMouseWhileInBounds, false, 10000, true); // activates bitmap hit & exit tracking
     trackMouseWhileInBounds(); // important: Immediate response, and sets _bitmapHit to correct state for event suppression.
    }
   }
   
   if (!_bitmapHit)
    event.stopImmediatePropagation();
  }
  
  
  protected function trackMouseWhileInBounds(event:Event=null):void
  {
   if (bitmapHitTest() != _bitmapHit)
   {
    _bitmapHit = !_bitmapHit;
    
    // Mouse is now on a nonclear pixel based on alphaTolerance. Reenable mouse events.
    if (_bitmapHit) {
     deactivateMouseTrap();
     setButtonModeCache(true, true);
     _transparentMode = false;
     super.mouseEnabled = true; // This will trigger rollOver & mouseOver events
    }
    
    // Mouse is now on a clear pixel based on alphaTolerance. Disable mouse events but .
    else if (!_bitmapHit) {
     _transparentMode = true;
     super.mouseEnabled = false; // This will trigger rollOut & mouseOut events
    }
   }
   
   // When mouse exits this MovieClip‘s bounds, end tracking & restore all.
   var localMouse:Point = _bitmapForHitDetection.localToGlobal(_mousePoint);
   if (hitTestPoint( localMouse.x, localMouse.y)==false) {
    removeEventListener(Event.ENTER_FRAME, trackMouseWhileInBounds);
    _transparentMode = false;
    setButtonModeCache(true);
    super.mouseEnabled = true;
    activateMouseTrap();
   }
  }
  
  
  protected function bitmapHitTest():Boolean {
   if (_bitmapForHitDetection==null)
    drawBitmapHitArea();
   _mousePoint.x = _bitmapForHitDetection.mouseX;
   _mousePoint.y = _bitmapForHitDetection.mouseY;
   return _bitmapForHitDetection.bitmapData.hitTest(_basePoint, _threshold, _mousePoint);
  }
  
  
  protected function setButtonModeCache(restore:Boolean=false, retain:Boolean=false) : void {
   if (restore) {
    if (_buttonModeCache==1)
     buttonMode = true;
    if (!retain)
     _buttonModeCache = NaN;
    return;
   }
   _buttonModeCache = (buttonMode==true ? 1 : 0);
   buttonMode = false;
  }
 }
}

原文地址:https://www.cnblogs.com/lingLuoChengMi/p/9264534.html

时间: 2024-10-08 15:03:12

as3 图片透明区域不接受鼠标事件的工具类的相关文章

Android ImageView图片透明区域不响应点击事件,不规则图片透明区域响应点击事件

转载:http://blog.csdn.net/aminfo/article/details/7872681 经常会在项目中用到透明图片,不规则图片,特别是做游戏的时候,需要对图片的透明区域的点击事件做特别处理. 一.先上图片文件transparent.png,图片中间区域与外围区域是非透明的,其它区域是透明的: 二.上布局文件test.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout

图片上传功能的几个工具类的介绍

(1)FTP上传文件的工具类: package com.taotao.common.utils; import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream

深入学习jQuery鼠标事件

前面的话 鼠标事件是DOM事件中最常用的事件,jQuery对鼠标事件进行了封装和扩展.本文将详细介绍jQuery鼠标事件 类型 鼠标事件共10类,包括click.contextmenu.dblclick.mousedown.mouseup.mousemove.mouseover.mouseout.mouseenter和mouseleave click 当用户按下并释放鼠标按键或其他方式"激活"元素时触发 contextmenu 可以取消的事件,当上下文菜单即将出现时触发.当前浏览器在鼠

图片压缩,裁切,水印工具类

package default; import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileInputStream;import java.io.InputStream; import javax.imageio.ImageIO; import net.coobird.thumbnailator.Thumbnails;import net.coobird.thumbnailator.geometry.Po

javafx的鼠标事件对于带有透明的图片在部分区域无效

JavaFX ImageView 中存储一个带有透明区域的图片时,当鼠标位于透明区域上方时,点击或拖拽并不会触发鼠标事件. 例如我有一个这样的图标,通过代码添加到 ImageView 中,并注册点击事件. TreeItem treeItem = new TreeItem(); ImageView imageView = new ImageView(new Image("/lin.png")); treeItem.setGraphic(imageView);imageView.setOn

【练习4.2】使用鼠标事件获取图片像素值

<学习OpenCV>中文版第4章第2题 题目要求: 点击图片是获取该点的颜色值,并在图像上点击鼠标处用文本将颜色值显示出来. 程序代码: 1 #include "stdafx.h" 2 #include <cv.h> 3 #include <highgui.h> 4 using namespace std; 5 using namespace cv; 6 7 void MouseCallBack(int event, int x, int y, in

黑马day18 鼠标事件&amp;amp;图片变大

有时候我们在淘宝网或者京东商城上浏览要购买的商品的时候当把鼠标移动到图图片上的时候会发现图片放大.然后鼠标移动,图片也会跟着移动,接下来我就使用jquery来实现这样的效果: 这是图片文件夹: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html x

黑马day18 鼠标事件&amp;图片变大

有时候我们在淘宝网或者京东商城上浏览要购买的商品的时候当把鼠标移动到图图片上的时候会发现图片放大,然后鼠标移动,图片也会跟着移动,接下来我就使用jquery来实现这种效果: 这是图片目录: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xml

基于OpenGL编写一个简易的2D渲染框架-07 鼠标事件和键盘事件

这次为程序添加鼠标事件和键盘事件 当检测到鼠标事件和键盘事件的信息时,捕获其信息并将信息传送到需要信息的对象处理.为此,需要一个可以分派信息的对象,这个对象能够正确的把信息交到正确的对象. 实现思路: 要实现以上的功能,需要几个对象: 事件分派器:EventDispatcher,负责将 BaseEvent 分派给 EventListener 对象 事件监听器:EventListener,这只是一个接口类,接受 BaseEvent 的对象,真正的处理在它的子类中实现 事件:BaseEvent,储存