Manipulating filter effects with code 531
Setting the filters property duplicates the filters array passed in and does not store it as a
reference. When getting the filters property, it returns a new copy of the array. One negative
implication of this approach is that the following code does not work:
// does not work
my_mc.filters[0].blurX = 20;
Because the previous code snippet returns a copy of the filters array, the code modifies the
copy instead of the original array. In order to modify the blurX property, you would need to
use the following ActionScript code instead:
// works
var filterArray:Array = my_mc.filters;
filterArray[0].blurX = 20;
my_mc.filters = filterArray;
The following procedure blurs an image based on the current position of the mouse pointer
on the Stage. Whenever the mouse pointer moves horizontally or vertically, the
blurX and
blurY properties of the blur filter are modified accordingly.
To adjust a movie clip’s filter properties:
1. Create a new Flash document and save it as adjustfilter.fla.
2. Add the following ActionScript to Frame 1 of the Timeline:
import flash.filters.BlurFilter;
this.createEmptyMovieClip("holder_mc", 10);
holder_mc.createEmptyMovieClip("img_mc", 20);
holder_mc.img_mc.loadMovie("http://www.helpexamples.com/flash/images/
image2.jpg");
holder_mc.filters = [new BlurFilter(10, 10, 2)];
holder_mc._x = 75;
holder_mc._y = 75;
holder_mc.onMouseMove = function() {
var tempFilter:BlurFilter = holder_mc.filters[0];
tempFilter.blurX = Math.floor((_xmouse / Stage.width) * 255);
tempFilter.blurY = Math.floor((_ymouse / Stage.height) * 255);
holder_mc.filters = [tempFilter];
};
The previous code is split into three sections. The first section imports the
flash.filters.BlurFilter class so that you don’t have to use the fully qualified class name
when you refer to the BlurFilter class. The second section of code creates a couple of
movie clips and loads an image into one of the nested clips. The third section of code
responds to the mouse movement on the Stage and adjusts the blur accordingly.
3. Select Control > Test Movie to test the Flash document.