Return to Snippet

Revision: 68726
at February 16, 2015 18:36 by SamKantor


Updated Code
/*
Created by Sam Kantor
Example Usage:
if (ObjectsHit(myobject, myotherobject)) {
The Objects hit!
}
*/
function ObjectsHit(mc1, mc2):Boolean {
	var mc1bounds = mc1.getBounds(this);
	var mc2bounds = mc2.getBounds(this);
	var allintersections = (mc2bounds.intersection(mc1bounds));	
	for (var xval:Number = allintersections.x; xval < allintersections.x + allintersections.width; xval ++ ) {
		for (var yval:Number = allintersections.y; yval < allintersections.y + allintersections.height;yval ++ ) {
			if (mc2.hitTestPoint(xval, yval, true) && mc1.hitTestPoint(xval, yval, true)) {
				return(true);
			}
		}
	}
	
	return(false);

Revision: 68725
at February 16, 2015 16:05 by SamKantor


Updated Code
/*
Created by Sam Kantor
Example Usage:
if (ObjectsHit(myobject, myotherobject)) {
The Objects hit!
}
*/
function ObjectsHit(mc1, mc2):Boolean {
	mc1bounds = mc1.getBounds(this);
	mc2bounds = mc2.getBounds(this);
	var allintersections = (mc2bounds.intersection(mc1bounds));	
	for (var xval:Number = allintersections.x; xval < allintersections.x + allintersections.width; xval ++ ) {
		for (var yval:Number = allintersections.y; yval < allintersections.y + allintersections.height;yval ++ ) {
			if (mc2.hitTestPoint(xval, yval, true) && mc1.hitTestPoint(xval, yval, true)) {
				return(true);
			}
		}
	}
	
	return(false);

Revision: 68724
at February 16, 2015 15:30 by SamKantor


Initial Code
/*
Snippet by Sam Kantor
Example Usage:
if (objectshit(myobject, myotherobject)) {
The Objects hit!
}
*/
function objectshit(mc1, mc2):Boolean {
	mc1bounds = mc1.getBounds(this);
	mc2bounds = mc2.getBounds(this);
	var allintersections = (mc2bounds.intersection(mc1bounds));	
	for (var xval:Number = allintersections.x; xval < allintersections.x + allintersections.width; xval ++ ) {
		for (var yval:Number = allintersections.y; yval < allintersections.y + allintersections.height;yval ++ ) {
			if (mc2.hitTestPoint(xval, yval, true) && mc1.hitTestPoint(xval, yval, true)) {
				return(true);
			}
		}
	}
	
	return(false);

Initial URL


Initial Description
A fast and accurate Hit Test that can check if objects truly touch each other.
This differs from HitTestObject, as it actually checks if objects visually hit, rather then their boundary boxes. Include this function in your code, for easy, efficient and effortless collision detection!

Initial Title
Pixel Perfect Collision Detection AS3

Initial Tags


Initial Language
ActionScript 3