Sunday, March 23, 2008


View SWF online
Download source
Download API

Having a DisplayObject shasing the mouse cursor or another DisplaObject is a pretty common effect seen in lots of Flash movies and games.

Since I'm too lazy to type down the same code again and again in every Flash file,
I decided to make this function into an API and eliminate this chore once and for all.

Chaser is quite easy to use, really.
Here's a simple piece of code:

import idv.cjcat.utils.*;

Chaser.addChaser(chaser, target, ChaserMode.ATTRACT, 0.5);
Chaser.chaserInfo.addEventListener(ChaserEvent.REACHED, reached);

function reached(e:ChaserEvent):void {
trace("chaser's raeched its target at: (" + e.chaser.x " + ", " + e.chaser.y + ")");

Both chaser and target are DisplayObjects.
If target has a null value, which is default, the chaser would chase the mouse cursor, instead.

There are two ChaserModes:
1. DIRECT - the chaser's velocity always points to its target.
2. ATTRACT - the chaser's acceleration always points to its target, which is more like a "bouncy spring" effect.

Hope you find this little tool handy :)

No comments: