BitmapDataとhitTest
/**
* Copyright hoooooonda ( http://wonderfl.net/user/hoooooonda )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/tZcC
*/
// forked from hoooooonda's hitTest1
// forked from hoooooonda's パーティクルと当たり判定
package {
import flash.geom.Rectangle;
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 var bmpd1:BitmapData;
public var bmpd2:BitmapData;
public var resetRect:Rectangle;
public var rect1:Rectangle;
public var rect2:Rectangle;
public var pos1:Point;
public var pos2:Point;
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();
bmp1.bitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0x00000000);
addChild(bmp1);
bmp2 = new Bitmap();
bmp2.bitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0x00000000);
addChild(bmp2);
resetRect = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
pos1 = new Point(0, 0);
pos2 = new Point(0, 0);
addEventListener(Event.ENTER_FRAME, loop);
}
public function loaded(e:Event):void
{
var bmpd:BitmapData;
var rect:Rectangle;
var holder:Sprite = new Sprite();
holder.addChild(e.target.loader);
bmpd = new BitmapData(holder.width, holder.height, true, 0x00000000);
bmpd.draw(holder);
rect = new Rectangle(0, 0, holder.width, holder.height);
if(e.target.loader == loader)
{
bmpd1 = bmpd;
rect1 = rect;
}else
{
bmpd2 = bmpd;
rect2 = rect;
}
}
public function loop(e:Event):void
{
pos1.x = mouseX - bmpd1.width/2;
pos1.y = mouseY - bmpd1.height/2;
pos2.x = stage.stageWidth/2 - bmpd2.width/2;
pos2.y = stage.stageHeight/2 - bmpd2.height/2;
bmp1.bitmapData.fillRect(resetRect, 0x00000000);
bmp1.bitmapData.copyPixels(bmpd1, rect1, pos1, null, null, true);
bmp2.bitmapData.fillRect(resetRect, 0x00000000);
bmp2.bitmapData.copyPixels(bmpd2, rect2, pos2, null, null, true);
if(bmpd1.hitTest(pos1, 255, bmpd2, pos2, 255))
{
bmp1.alpha = 0.5;
}else
{
bmp1.alpha = 1;
}
}
}
}