网上的一般的方法为~
先获取其bitmapData对象..
当进行点击操作时使用..
bitmapData.getPixel32(x,y)获取..
点击位置的是否透明..
然后再操作...
而mosueOver与mouseOut等事件稍为复杂点..
需要侦听mouseMove..
然后对经过的坐标进行透明判断~~再抛出相应的事件...
今天介绍的方法原理跟上面差不多..
不过我们使用Sprite的另一个属性hitArea..(关于hitArea的说明请查看这里)
在图像加载完毕后..
先绘制一个去除了透明部份的Sprite..
然后把该Sprite指定为源对象的hitArea.
代码也很简单:
main.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="*">
- <mx:Script>
- <![CDATA[
- import mx.managers.CursorManager;
- private function abc(e:MouseEvent):void
- {
- if(e.type == "mouseOver")
- {
- CursorManager.setBusyCursor();
- }else
- {
- CursorManager.removeBusyCursor();
- }
- }
- ]]>
- </mx:Script>
- <local:MyImage source="10020601.png" mouseOver="abc(event)" mouseOut="abc(event)" y="25"/>
- <mx:Image source="10020601.png" mouseOver="abc(event)" mouseOut="abc(event)" x="344" y="25"/>
- </mx:Application>
- package
- {
- import flash.display.BitmapData;
- import flash.display.DisplayObject;
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.geom.Matrix;
- import flash.utils.setTimeout;
- import mx.controls.Image;
- /**
- * 自定义Image类,使之忽略PNG透明区域
- * @author L4cd.Net
- */
- public class MyImage extends Image
- {
- private var ht:Sprite = new Sprite();
- public function MyImage(){
- addChild(ht);
- hitArea = ht;
- //指定hitArea为ht对象
- ht.visible = false;
- ht.mouseEnabled = false;
- mouseChildren = false;
- addEventListener(Event.COMPLETE,complete,false,99,true);
- setTimeout(update,50)
- }
- private function complete(e:Event):void{
- setTimeout(update,50)
- }
- private function update():void{
- if(!content)return;
- var loader:DisplayObject = content.parent as DisplayObject;
- var bit:BitmapData = new BitmapData(loader.width,loader.height,true,0x00000000);
- var mat:Matrix = new Matrix();
- mat.scale(loader.scaleX,loader.scaleY);
- bit.draw(loader,mat);
- //重绘图象到bit
- ht.graphics.clear();
- ht.graphics.beginFill(0);
- for(var x:uint=0;x<bit.width;x++){
- for(var y:uint=0;y<bit.height;y++){
- if(bit.getPixel32(x,y))ht.graphics.drawRect(x,y,1,1);
- }
- }
- //以graphics画出bit的无透明区域
- ht.graphics.endFill();
- }
- }
- }