Flash CS3 Documentation |
|||
| Programming ActionScript 3.0 > Filtering display objects > Creating and applying filters > Applying a filter | |||
Once you've constructed a filter object, you can apply it to a display object or a BitmapData object; how you apply the filter depends on the object to which you're applying it.
When you apply filter effects to a display object, you apply them through the filters property. The filters property of a display object is an Array instance, whose elements are the filter objects applied to the display object. To apply a single filter to a display object, create the filter instance, add it to an Array instance, and assign that Array object to the display object's filters property:
import flash.display.Bitmap; import flash.display.BitmapData; import flash.filters.DropShadowFilter; // Create a bitmapData object and render it to screen var myBitmapData:BitmapData = new BitmapData(100,100,false,0xFFFF3300); var myDisplayObject:Bitmap = new Bitmap(myBitmapData); addChild(myDisplayObject); // Create a DropShadowFilter instance. var dropShadow:DropShadowFilter = new DropShadowFilter(); // Create the filters array, adding the filter to the array by passing it as // a parameter to the Array() constructor. var filtersArray:Array = new Array(dropShadow); // Assign the filters array to the display object to apply the filter. myDisplayObject.filters = filtersArray;
If you want to assign multiple filters to the object, simply add all the filters to the Array instance before assigning it to the filters property. You can add multiple objects to an Array by passing them as parameters to its constructor. For example, this code applies a bevel filter and a glow filter to the previously created display object:
import flash.filters.BevelFilter; import flash.filters.GlowFilter; // Create the filters and add them to an array. var bevel:BevelFilter = new BevelFilter(); var glow:GlowFilter = new GlowFilter(); var filtersArray:Array = new Array(bevel, glow); // Assign the filters array to the display object to apply the filter. myDisplayObject.filters = filtersArray;
|
NOTE |
|
When you're creating the array containing the filters, you can create it using the var filters:Array = new Array(dropShadow, blur);
does the same thing as this line of code: var filters:Array = [dropShadow, blur];
|
If you apply multiple filters to display objects, they are applied in a cumulative, sequential manner. For example, if a filters array has two elements, a bevel filter added first and a drop shadow filter added second, the drop shadow filter is applied to both the bevel filter and the display object. This is because of the drop shadow filter's second position in the filters array. If you want to apply filters in a noncumulative manner, you must apply each filter to a new copy of the display object.
If you're only assigning one or a few filters to a display object, you can create the filter instance and assign it to the object in a single statement. For instance, the following line of code applies a blur filter to a display object called myDisplayObject:
myDisplayObject.filters = [new BlurFilter()];
The previous code creates an Array instance using Array literal syntax (square braces), creates a new BlurFilter instance as an element in the Array, and assigns that Array to the filters property of the display object named myDisplayObject.
Removing all filters from a display object is as simple as assigning a null value to the filters property:
myDisplayObject.filters = null;
If you've applied multiple filters to an object and want to remove only one of the filters, you must go through several steps to change the filters property array. For more information, see Changing filters at run time.
Applying a filter to a BitmapData object requires the use of the BitmapData object's applyFilter() method:
myBitmapData.applyFilter(sourceBitmapData);
The applyFilter() method applies a filter to a source BitmapData object, producing a new, filtered image. This method does not modify the original source image; instead, the result of the filter being applied to the source image is stored in the BitmapData instance on which the applyFilter() method is called.
Flash CS3
RSS feed | Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flash/9.0/main/00000194.html
Comments
BarkinB said on May 2, 2007 at 2:08 PM :