In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

BitmapとhitTest

Get Adobe Flash player
by hoooooonda 07 Jul 2011
/**
 * Copyright hoooooonda ( http://wonderfl.net/user/hoooooonda )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/mZBQ
 */

// forked from hoooooonda's パーティクルと当たり判定
package {
    import flash.geom.Matrix;
    import flash.net.URLRequest;
    import flash.utils.Proxy;
    import flash.geom.Point;
    import flash.ui.Mouse;
    import flash.events.*;
    import flash.display.*;
    import flash.system.*;
    public class FlashTest extends Sprite {
        public var loader:Loader;
        public var loader2:Loader;    
        public var bmp1:Bitmap;
        public var bmp2:Bitmap;
        
        public function FlashTest() {
           Security.loadPolicyFile("http://hr-portfolio.main.jp/crossdomain.xml"); 
                      
           loader = new Loader();
           loader.load(new URLRequest("http://hr-portfolio.main.jp/test.png"));
           loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);

           loader2 = new Loader();
           loader2.load(new URLRequest("http://hr-portfolio.main.jp/test2.png"));
           loader2.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
           
           bmp1 = new Bitmap();
           addChild(bmp1);
           bmp2 = new Bitmap();
           addChild(bmp2);
           addEventListener(Event.ENTER_FRAME, loop);
        }
        
        public function loaded(e:Event):void
        {
           var bmp:Bitmap;
           if(e.target.loader == loader)
           {
              bmp = bmp1;
           }else
           {
              bmp = bmp2;               
           }
           
           var holder:Sprite = new Sprite();
           holder.addChild(e.target.loader);
           
           var bmpd:BitmapData = new BitmapData(holder.width, holder.height, true, 0x00000000);           
           bmpd.draw(holder);
           bmp.bitmapData = bmpd;
           
           bmp.x = stage.stageWidth/2 - bmp.width/2;
           bmp.y = stage.stageHeight/2 - bmp.height/2; 
        }
        
        public function loop(e:Event):void
        {
            bmp1.x = mouseX - bmp1.width/2;
            bmp1.y = mouseY - bmp1.height/2;
            
            if(bmp1.bitmapData.hitTest(new Point(bmp1.x, bmp1.y), 255, bmp2.bitmapData, new Point(bmp2.x, bmp2.y), 255))
            {
                bmp1.alpha = 0.5;
            }else
            {
                bmp1.alpha = 1;
            }
        }

    }
}