ActionScript 3.0: BitmapData, Pixel Level Hit Detection

How to use BitmapData’s hitTest to do pixel level hit detection against another bitmap.


class Actor extends Sprite {
    public var someBitmap:Bitmap;
}

var src:Actor = new Actor();
var target:Actor = new Actor(); 

// bm is BitmapData.
var bm:BitmapData = obj.someBitmap.bitmapData;

// target is BitmapData
var bm2:BitmapData = target.someBitmap.bitmapData;

// this checks if src object collides with target object.
if (bm.hitTest(new Point(src.x, src.y), 255, bm2, new Point(target.x, target.y))) {
    trace('hit!');
}

Bitmap Animation In Flex 3 – ActionScript 3.0

Click on the Flash and press the LEFT ARROW if you don’t see anything.
Here’s a snippet of code that will allow you to animate a sprite within Flex 3.0. Each PNG file is 512×64 with 8 images spread out horizontally. I used 0xFF00FF as the transparency colour. Press arrow keys to move the character around — I did not add bounds checking so he’ll walk right off the screen. :p

The basic premise of this class is that it’s a sprite class with a bitmap object attached to it. The bitmap object copies a portion of its image from a set of BitmapData objects. There are 4 sets of PNGs, each representing a direction of movement and contain frame animation for the movement. As the frames advance the bitmap moves the ‘frame’ along the BitmapData whic effectively shows the next frame in the animation.


Source Code Follows:
Continue reading