Moving a DropShadow with the Mouse
If you would like to move a DropShadow, or just about anything else for that matter, as the user moves the Mouse around then try something like this: ( download FLA )
The code in the FLA is very well commented. Here’s the gist of what’s going on:
1 – Start by creating a dynamic DropShadow
import flash.filters.DropShadowFilter;
var ds:DropShadowFilter = new DropShadowFilter(15, 0, 0x000000, 0.8, 5, 5, 1, 3, false, false, false);
myText.filters = [ ds ]; //
This last line is a bit of a cheat because we are passing ds as an array and not pushing it on to the myText.filters collection. We can do this because we only have one filter that we’re using here.
2 – Create a Mouse Listener
import mx.utils.Delegate;
var mouseList:Object = new Object();
mouseList.onMouseMove = Delegate.create( this, updateShadow );
Mouse.addListener( mouseList );
This should look familiar if you’ve created Mouse Listeners before. What might be a little different is the use of the Delegate class. It’s an amazing class that allows you to run functions in a specified scope. Otherwise the listener would be running in the scope of the mouseList object which is pretty useless. Delegate allows us to keep the scope as this so that we can still access functions on our frame here.
Delegate shines even more when you use it with classes. Once you start using it you’ll love it. You can read a bit more about it on polyGeek.com.
3 – Create a function that is called whenever the Mouse is moved
function updateShadow():Void {
var angle:Number = getAngleBetweenPoints();
angle = (angle * 180) / Math.PI;
ds = new DropShadowFilter(15, angle, 0x000000, 0.8, 5, 5, 1, 3, false, false, false);
myText.filters = [ ds ];
}
This function is called every time the Mouse moves.
4 – Create a function that returns the angle between two points. If you don’t pass any points then it will use the position of the Mouse and the center of the Stage as the two reference points.
function getAngleBetweenPoints( x1:Number, y1:Number, x2:Number, y2:Number ):Number {
if( x1 == undefined ) {
x1 = _xmouse;
y1 = _ymouse;
x2 = Stage.width / 2;
y2 = Stage.height / 2;
}
var dx:Number = y2 - x1;
var dy:Number = x2 - y1;
var angle:Number = Math.atan2( dy, dx );
return angle;
}
You have to love atan2. For more about this read Keith Peter’s Making Things Move, page 55.
















