Ignore events in PNG transparent regions (AS/flash)
Last Update:2018-12-07
Source: Internet
Author: User
Code : Pngloader. asview 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, mainly used to solve the issue of events in transparent pixels of PNG images * @ 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); // redraw the image to 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,) ;}// use graphics to draw a bit non-transparent hit. graphics. endfill () ;}} pngloaderexample. asview plaincopy to clipboardp RINT? 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. lo AD (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, 25 0); graphics. drawrect (320,20, 250,250);} private function o (E: mouseevent): void {e.tar get. Filters = (E. type = mouseevent. roll_over )? [New glowfilter ()]: [] ;}}