// forked from JuanSierra's flash on 2009-4-21 package { import flash.display.Sprite; import flash.events.*; import flash.display.*; import flash.text.*; import flash.events.*; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; [SWF(backgroundColor="0x333333", frameRate="30")] public class FlashTest extends Sprite { private var beam:Sprite = new Sprite(); private var a:Number=0.08; private var v:Number=0.05; public function FlashTest() { var anchoBeam:int = 100; var numLineas:int = 5; for(var i:int=0; i<numLineas; i++) { beam.graphics.lineStyle(1,0xff0000); beam.graphics.moveTo((anchoBeam/numLineas)*i,0); beam.graphics.lineTo((anchoBeam/numLineas)*i, 40); } addChild(beam); beam.x=100; addEventListener(Event.ENTER_FRAME,pintar); } private function pintar(event:Event):void { if(beam.y<400) { v+=a; beam.y+=v; beam.x=5*Math.sin(beam.y); } } } } class HitTest { import flash.display.DisplayObject; import flash.display.BitmapData; import flash.geom.ColorTransform; import flash.geom.Matrix; import flash.geom.Rectangle; /** * hitTestObject checks to see if two display objects have collided from * http://www.adventuresinactionscript.com/blog/15032008/actionscript-3-hit... * Based on Grant Skinner & Troy Gilberts collision detection functions * * @param object1 the first object to be tested * @param object2 the second object to be tested * @param pixelPerfect check bounding rectangle only if set to false * @param tolerance alpha tolerance value * @return intersecting rectangle if the objects have collided, or null if no collision * @see hitTestObject */ static public function hitTestObject(object1:DisplayObject, object2:DisplayObject, pixelPerfect:Boolean=true, tolerance:int = 255):Rectangle { // quickly rule out anything that isn't in our hitregion if (object1.hitTestObject(object2)) { // get bounds: var bounds1:Rectangle = object1.getBounds(object1.parent); var bounds2:Rectangle = object2.getBounds(object2.parent); // determine test area boundaries: var bounds:Rectangle = bounds1.intersection(bounds2); bounds.x = Math.floor(bounds.x); bounds.y = Math.floor(bounds.y); bounds.width = Math.ceil(bounds.width); bounds.height = Math.ceil(bounds.height); //ignore collisions smaller than 1 pixel if ((bounds.width < 1) || (bounds.height < 1)) return null; if (!pixelPerfect) return bounds; // set up the image to use: var img:BitmapData = new BitmapData(bounds.width, bounds.height, false); // draw in the first image: var mat:Matrix = object1.transform.concatenatedMatrix; mat.translate( -bounds.left, -bounds.top); img.draw(object1,mat, new ColorTransform(1,1,1,1,255,-255,-255,tolerance)); // overlay the second image: mat = object2.transform.concatenatedMatrix; mat.translate( -bounds.left, -bounds.top); img.draw(object2,mat, new ColorTransform(1,1,1,1,255,255,255,tolerance),"difference"); // find the intersection: var intersection:Rectangle = img.getColorBoundsRect(0xFFFFFFFF,0xFF00FFFF); // if there is no intersection, return null: if (intersection.width == 0) { return null; } // adjust the intersection to account for the bounds: intersection.offset(bounds.left, bounds.top); return intersection; } else return null; } } forked from: flash on 2009-4-21