To create a drop shadow that follows the mouse pointer:
1.
Create a new Flash document and save it as dropshadowmouse.fla.
2.
Add the following ActionScript to Frame 1 of the Timeline:
import flash.filters.DropShadowFilter;
System.security.allowDomain("http://www.helpexamples.com");
var dropShadow:DropShadowFilter = new DropShadowFilter(4, 45, 0x000000,
0.8, 10, 10, 2, 2);
// Load and position the image on the Stage.
var mclListener:Object = new Object();
mclListener.onLoadInit = function(target_mc:MovieClip):Void {
target_mc._x = (Stage.width - target_mc._width) / 2;
target_mc._y = (Stage.height - target_mc._height) / 2;
};
this.createEmptyMovieClip("img_mc", 10);
var img_mcl:MovieClipLoader = new MovieClipLoader();
img_mcl.addListener(mclListener);
img_mcl.loadClip("http://www.helpexamples.com/flash/images/image1.jpg",
img_mc);
// When mouse moves, recalculate the position of the drop shadow.
var mouseListener:Object = new Object();
mouseListener.onMouseMove = function():Void {
var p1:Number = img_mc._y - _ymouse;
var p2:Number = img_mc._x - _xmouse;
var degrees:Number = Math.atan2(p1, p2) / (Math.PI / 180);
dropShadow.distance = Math.sqrt(Math.pow(p1, 2) + Math.pow(p2, 2)) *
0.5;
dropShadow.blurX = dropShadow.distance;
dropShadow.blurY = dropShadow.blurX;
dropShadow.angle = degrees - 180;
img_mc.filters = [dropShadow];
};
Mouse.addListener(mouseListener);
The first section of this code defines a drop shadow instance, loads an external image, and
repositions the image at the center of the Stage. The second section of code defines a
mouse listener, which you call whenever the user moves the mouse pointer around the
Stage. Whenever the mouse moves, the event handler recalculates the distance and angle
between the mouse pointer and the upper-left corner of the image. Based on this
calculation, the drop shadow filter is reapplied to the movie clip.
3.
Select Control > Test Movie to test the Flash document.
When you run the SWF file, the drop shadow follows the mouse pointer. The closer you
move the mouse pointer to the upper-left corner of the image on the Stage, the less of a
blur effect is applied to the image. As the mouse pointer moves further from the upper-left
corner of the image, the drop shadow effect becomes more apparent.
512
Animation, Filters, and Drawings
Need help?
Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?