Event detection in pixels

Source: Internet
Author: User

Package <br/> {</P> <p> Import flash. display. bitmapdata; <br/> Import flash. display. blendmode; <br/> Import flash. display. displayobject; <br/> Import flash. display. sprite; </P> <p> Import flash. geom. colortransform; <br/> Import flash. geom. matrix; <br/> Import flash. geom. point; <br/> Import flash. geom. rectangle; </P> <p> public class hittest <br/>{</P> <p>/* <br/> This method returns true/false <br/> * /<br/> publi C static function complexhittestobject (target1: displayobject, TARGET2: displayobject, accurracy: Number = 1): Boolean <br/>{< br/> return complexintersectionrectangle (target1, TARGET2, accurracy ). width! = 0; <br/>}</P> <p> Public static function intersectionrectangle (target1: displayobject, TARGET2: displayobject ): rectangle <br/>{< br/> // if either of the items don't have a reference to stage, then they are not in a display list <br/> // or if a simple hittestobject is false, they cannot be intersecting. <br/> If (! Target1.root |! Target2.root |! Target1.hittestobject (TARGET2) return New rectangle (); </P> <p> // get the bounds of each displayobject. <br/> var bounds1: rectangle = target1.getbounds (target1.root); <br/> var bounds2: rectangle = target2.getbounds (target2.root ); </P> <p> // determine Test Area Boundaries. <br/> var intersection: rectangle = new rectangle (); <br/> intersection. X = math. max (bounds1.x, bounds2.x); <br/> interse Ction. y = math. max (bounds1.y, bounds2.y); <br/> intersection. width = math. min (bounds1.x + bounds1.width)-intersection. x, (bounds2.x + bounds2.width)-intersection. x); <br/> intersection. height = math. min (bounds1.y + bounds1.height)-intersection. y, (bounds2.y + bounds2.height)-intersection. y); </P> <p> return intersection; <br/>}</P> <p> Public static function complexintersecti Onrectangle (target1: displayobject, TARGET2: displayobject, accurracy: Number = 1): rectangle <br/>{< br/> If (accurracy <= 0) throw new error ("argumenterror: Error #5001: invalid value for accurracy", 5001); </P> <p> // If a simple hittestobject is false, they cannot be intersecting. <br/> If (! Target1.hittestobject (TARGET2) return New rectangle (); </P> <p> var hitrectangle: rectangle = intersectionrectangle (target1, TARGET2 ); <br/> // if their boundaries are no interesecting, they cannot be intersecting. <br/> If (hitrectangle. width * accurracy <1 | hitrectangle. height * accurracy <1) return New rectangle (); </P> <p> var bitmapdata: bitmapdata = new bitmapdata (hitrectangle. width * ACC Urracy, hitrectangle. height * accurracy, false, 0x000000); </P> <p> // draw the first target. <br/> bitmapdata. draw (target1, hittest. getdrawmatrix (target1, hitrectangle, accurracy), new colortransform (1, 1, 1, 1,255,-255,-255,255); <br/> // overlay the second target. <br/> bitmapdata. draw (TARGET2, hittest. getdrawmatrix (TARGET2, hitrectangle, accurracy), new colortransform (1, 1, 1, 1, 255,255,255,255), blendmode. difference); </P> <p> // find the intersection. <br/> var intersection: rectangle = bitmapdata. getcolorboundsrect (0 xffffffff, 0xff00ffff); </P> <p> bitmapdata. dispose (); </P> <p> // alter width and positions to compensate for accurracy <br/> If (accurracy! = 1) <br/>{< br/> intersection. x/= accurracy; <br/> intersection. y/= accurracy; <br/> intersection. width/= accurracy; <br/> intersection. height/= accurracy; <br/>}</P> <p> intersection. X + = hitrectangle. x; <br/> intersection. Y + = hitrectangle. y; </P> <p> return intersection; <br/>}</P> <p> protected static function getdrawmatrix (target: displayobject, hitrectangle: rectangle, accurracy: number ): Matrix <br/> {<br/> var localtoglobal: point; <br/> var matrix: matrix; </P> <p> var rootconcatenatedmatrix: matrix = target. root. transform. concatenatedmatrix; </P> <p> localtoglobal = target. localtoglobal (new point (); <br/> matrix = target. transform. concatenatedmatrix; <br/> matrix. tx = localtoglobal. x-hitrectangle. x; <br/> matrix. ty = localtoglobal. y-hitrectangle. y; </P> <p> matrix. A = matrix. A /Rootconcatenatedmatrix. A; <br/> matrix. d = matrix. d/rootconcatenatedmatrix. D; <br/> If (accurracy! = 1) matrix. scale (accurracy, accurracy); </P> <p> return matrix; <br/>}</P> <p>}

Hittest.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.