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

Flash / ActionScript + AMF + Zend Framework mini-How-To

There are times when you need your Flash applet to fetch data from an external source. The simplest way to do that with a PHP backend is a combination of Zend Framework’s AMF module and Flash’s built in AMF remoting feature (Action Message Format).

The steps to go about doing that is to have the client side flash component, and a server side PHP component with Zend Framework installed.
On the flash side, you create a netconnection object, assign the gateway as the URI of your web service (http://example.com/services/amf.php in this case). You specify a responder and set it to listen to any events. In the example below, I use JSON to encode the objects, but that’s optional, I just happened to be dealing with JSON objects at the time.

Source example follows:
Continue reading