Wednesday, 19 December 2012

Hit test object bounds bmp data in actionscript


1. create movieclip with the instance name of 'laserGun'
2. create movieclip with the instance name of 'redClip'



code :-


addEventListener(Event.ENTER_FRAME, enterFrame);

function enterFrame(e:Event):void
{
laserGun.x = mouseX;
laserGun.y = mouseY;


var laserRect:Rectangle = laserGun.getBounds(this);
var laserOffset:Matrix = laserGun.transform.matrix;
laserOffset.tx = laserGun.x - laserRect.x;
laserOffset.ty = laserGun.y - laserRect.y;

var laserGunBmpData = new BitmapData(laserRect.width,laserRect.height,true,0);
laserGunBmpData.draw(laserGun, laserOffset);

var redRect:Rectangle = redClip.getBounds(this);
var redClipBmpData = new BitmapData(redRect.width,redRect.height,true,0);

var redOffset:Matrix = redClip.transform.matrix;
redOffset.tx = redClip.x - redRect.x;
redOffset.ty = redClip.y - redRect.y;

redClipBmpData.draw(redClip, redOffset);

var rLoc:Point = new Point(redRect.x,redRect.y);
var bLoc:Point = new Point(laserRect.x,laserRect.y);

if (redClipBmpData.hitTest(rLoc,
255,
laserGunBmpData,
bLoc,
255
 ))
{
trace("Hit");

}
else
{
trace("No Hit");
}

laserGunBmpData.dispose();
redClipBmpData.dispose();
}

output look like :-





No comments:

Post a Comment