OK, over the last few days, I have been desperately searching for a way to do pixel perfect collisions, and for someone to explain it to me in detail. I finally have enough information(special thanks to stringy) to explain all what I’ve learned, in great detail, and explain the code in the .fla file, posted by the Canadian in the frequently asked questions thread on bitmapData hit testing:
//This code imports the necessary classes:
import flash.geom.Point;
import flash.display.BitmapData;
//This code checks every frame wether the red movie clip is hitting the blue
//movie clip, using the pixelHitTest function that you will define later:
this.onEnterFrame = function():Void {
if (red.pixelHitTest(blue)) {
this.red._alpha = this.blue._alpha = 50;
} else {
this.red._alpha = this.blue._alpha = 100;
}
}
//Takes care of the dragging:
this.red.onPress = this.blue.onPress = startDrag;
this.red.onRelease = this.blue.onRelease = stopDrag;
//Defines the pixelHitTest function as a prototype, meaning that you can
//call the function like this: "movieclip.pixelHitTest..."
//Putting "MovieClip" before prototype means that what you put before the
//function has to be a movie clip.
//The functions arguments are mc, which must be a movie clip, and
//threshold, which is the alpha threshold, and must be a number.
//Putting :Boolean afterwards, means that it will return only a boolean value
MovieClip.prototype.pixelHitTest = function(mc:MovieClip, threshold:Number):Boolean {
//Now for the code inside the function. This checks if threshold is equal to
//threshold(i.e. if the user has typed it while calling the function), and if
//it is, keeps it that way. If it isn't, it will, by default, be equal to 1:
threshold = threshold ? threshold : 1;
//This code sets two variables, thisBitmap, and mcBitmap. Inside them, it
//draws this movieClip, (i.e. the one before the function when it is called,
//thanks to prototype) and the mc movieClip:
var thisBitmap:BitmapData = new BitmapData(this._width, this._height, true, 0);
thisBitmap.draw(this);
var mcBitmap:BitmapData = new BitmapData(mc._width, mc._height, true, 0);
mcBitmap.draw(mc);
//Checks if the Bitmaps hit, and tells it to return true if they do, and false if
//they don't:
if(thisBitmap.hitTest(new Point(this._x, this._y), threshold, mcBitmap, new Point(mc._x, mc._y), threshold)) {
return true;
}
return false;
}
You may want to know the structure of that advanced hit test function, as I did. It is:
Bitmap1.hitTest(Top Left point of movieClip1, alpha threshold of Bitmap1, Bitmap2, Top left point of MovieClip2, alpha threshold of Bitmap2)
Alpha threshold, was well explained to me by stringy, so I’ll quote him:
[quote=stringy;2095977]…after looking through the help files,i must admit they are terribly vague.
All you are doing is checking if _alpha is above a certain amount. With movieClips you would use 0->100 but with this hittest you have to use 0->255 (0x00->0xFF)so an _alpha of 50 would correspond to approx 128 (0x80).
Nearly all the time you could just use 1 as a hittest parameter so you are checking for any hit at all but maybe you have a shadow in a mc layer and you do not want to check a hit. This is when you might be able to use your threshold parameter.[/quote]
In other words, the hit test will only react to parts of the bitmap which have a greater or equal alpha than the alpha threshold, mostly useful for a shadow.
I hope this has explained briefly what it took me ages to find out.