13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Filtering display objects<br />

import flash.display.Bitmap;<br />

import flash.display.BitmapData;<br />

import flash.filters.DropShadowFilter;<br />

// Create a bitmapData object and r<strong>en</strong>der it to scre<strong>en</strong><br />

var myBitmapData:BitmapData = new BitmapData(100,100,false,0xFFFF3300);<br />

var myDisplayObject:Bitmap = new Bitmap(myBitmapData);<br />

addChild(myDisplayObject);<br />

// Create a DropShadowFilter instance.<br />

var dropShadow:DropShadowFilter = new DropShadowFilter();<br />

// Create the filters array, adding the filter to the array by passing it as<br />

// a parameter to the Array() constructor.<br />

var filtersArray:Array = new Array(dropShadow);<br />

// Assign the filters array to the display object to apply the filter.<br />

myDisplayObject.filters = filtersArray;<br />

If you want to assign multiple filters to the object, simply add all the filters to the Array instance before assigning it to<br />

the filters property. You can add multiple objects to an Array by passing them as parameters to its constructor. For<br />

example, this code applies a bevel filter and a glow filter to the previously created display object:<br />

import flash.filters.BevelFilter;<br />

import flash.filters.GlowFilter;<br />

// Create the filters and add them to an array.<br />

var bevel:BevelFilter = new BevelFilter();<br />

var glow:GlowFilter = new GlowFilter();<br />

var filtersArray:Array = new Array(bevel, glow);<br />

// Assign the filters array to the display object to apply the filter.<br />

myDisplayObject.filters = filtersArray;<br />

Wh<strong>en</strong> you’re creating the array containing the filters, you can create it using the new Array() constructor (as shown<br />

in the previous examples) or you can use Array literal syntax, wrapping the filters in square brackets ([]). For instance,<br />

this line of code:<br />

var filters:Array = new Array(dropShadow, blur);<br />

does the same thing as this line of code:<br />

var filters:Array = [dropShadow, blur];<br />

If you apply multiple filters to display objects, they are applied in a cumulative, sequ<strong>en</strong>tial manner. For example, if a<br />

filters array has two elem<strong>en</strong>ts, a bevel filter added first and a drop shadow filter added second, the drop shadow filter<br />

is applied to both the bevel filter and the display object. This is because of the drop shadow filter’s second position in<br />

the filters array. If you want to apply filters in a noncumulative manner, apply each filter to a new copy of the display<br />

object.<br />

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<br />

object in a single statem<strong>en</strong>t. For example, the following line of code applies a blur filter to a display object called<br />

myDisplayObject:<br />

myDisplayObject.filters = [new BlurFilter()];<br />

The previous code creates an Array instance using Array literal syntax (square braces), creates a BlurFilter instance as<br />

an elem<strong>en</strong>t in the Array, and assigns that Array to the filters property of the display object named<br />

myDisplayObject.<br />

Last updated 6/6/2012<br />

269

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!