·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]as3下实现png事件的操作

时间:2016-06-14 22:28酷播
忽略PNG透明区域的事件(AS/Flash).[AS3]as3下实现png事件的操作
代码:
PNGLoader.as
view plaincopy to clipboardprint?
package net.l4cd.display  
{  
    import flash.display.BitmapData;  
    import flash.display.Loader;  
    import flash.display.Sprite;  
    import flash.events.Event;  
    import flash.events.IOErrorEvent;  
    import flash.events.ProgressEvent;  
    import flash.geom.Matrix;  
    import flash.net.URLRequest;  
    import flash.system.LoaderContext;  
    import flash.utils.ByteArray;  
  
    /** 
     * PNGLoader,主要解决png图片透明像素处事件的问题 
     * @author L4cd.Net 
     * 
     */  
    public class PNGLoader extends Sprite  
    {  
        private var loader:Loader = new Loader();  
        private var hit:Sprite = new Sprite();  
        public function PNGLoader()  
        {  
            addChild(loader);  
            addChild(hit);  
            hit.visible = false;  
            hit.mouseEnabled = false;  
            mouseChildren = false;  
            hitArea = hit;  
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE,complete);  
            loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,error);  
            loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,progress);  
        }  
        private function complete(e:Event):void  
        {  
            dispatchEvent(e);  
            update();  
        }  
        private function error(e:IOErrorEvent):void  
        {  
            dispatchEvent(e);  
        }  
        private function progress(e:ProgressEvent):void  
        {  
            dispatchEvent(e);  
        }  
        public function load(request:URLRequest,context:LoaderContext=null):void  
        {  
            loader.load(request,context);  
            clear();  
        }  
        public function loadBytes(bytes:ByteArray,context:LoaderContext=null):void  
        {  
            loader.loadBytes(bytes,context);  
            clear();  
        }  
        public function unload():void  
        {  
            loader.unload();  
            clear();  
        }  
        public function close():void  
        {  
            loader.close();  
            clear();  
        }  
        private function clear():void  
        {  
            hit.graphics.clear();  
        }  
        private function update():void  
        {  
            if(!loader.content)return;  
            var bit:BitmapData = new BitmapData(loader.width,loader.height,true,0x00000000);  
            bit.draw(loader);  
            //重绘图象到bit  
            clear();  
            hit.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))hit.graphics.drawRect(x,y,1,1);  
                }  
            }  
            //以graphics画出bit的无透明区域  
            hit.graphics.endFill();  
        }  
    }  
}  
PNGLoaderExample.as
view plaincopy to clipboardprint?
package  
{  
    import flash.display.Loader;  
    import flash.display.Sprite;  
    import flash.events.MouseEvent;  
    import flash.filters.GlowFilter;  
    import flash.net.URLRequest;  
  
    import net.l4cd.display.PNGLoader;  
  
    [SWF(width="600",height="400")]  
    /** 
     * PNGLoaderExample 
     * @author L4cd.Net 
     * 
     */  
    public class PNGLoaderExample extends Sprite  
    {  
        public function PNGLoaderExample()  
        {  
            var pl:PNGLoader = new PNGLoader();  
            pl.load(new URLRequest("10020601.png"));  
            addChild(pl);  
            pl.y = 20;  
            pl.addEventListener(MouseEvent.ROLL_OUT,o);  
            pl.addEventListener(MouseEvent.ROLL_OVER,o);  
  
            var ld:Loader = new Loader();  
            ld.load(new URLRequest("10020601.png"));  
            addChild(ld);  
            ld.x = 320;  
            ld.y = 20;  
            ld.addEventListener(MouseEvent.ROLL_OUT,o);  
            ld.addEventListener(MouseEvent.ROLL_OVER,o);  
  
            graphics.lineStyle(1);  
            graphics.drawRect(20,20,250,250);  
  
            graphics.drawRect(320,20,250,250);  
        }  
        private function o(e:MouseEvent):void  
        {  
            e.target.filters = (e.type == MouseEvent.ROLL_OVER)?[new GlowFilter()]:[];  
        }  
    }  

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器