MySQL tidbits: IF statement

A useful (but seemingly underused) SQL keyword is the IF statement.

The usage is as follows:

SELECT IF(expr1, expr2, expr3);

If expr1 evalues to true, then expr2 is executed, otherwise, expr3 is. This is useful for doing conditional queries inside the SQL without having to put logic in your script/program.

SELECT IF( (SELECT name FROM people) = 'john'), (SELECT age FROM people WHERE name = 'john') , 'no age');

The above statement will return the age of the user ‘john’ if he exists in the people table. If not the query will return ‘no age.’
The caveat is that expr2 and expr3 must evaluate to a single value. The braces around expr2′s SELECT statement are required.

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!');
}