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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

Filtering display objects<br />

// Example of removing the top-most filter from a display object<br />

// named "filteredObject".<br />

var tempFilters:Array = filteredObject.filters;<br />

// Remove the last elem<strong>en</strong>t from the Array (the top-most filter).<br />

tempFilters.pop();<br />

// Apply the new set of filters to the display object.<br />

filteredObject.filters = tempFilters;<br />

Similarly, to remove the bottom-most filter (the first one applied to the object) you use the same code, substituting the<br />

Array class’s shift() method in place of the pop() method.<br />

To remove a filter from the middle of an array of filters (assuming that the array has more than two filters) you can use<br />

the splice() method. You must know the index (the position in the array) of the filter you want to remove. For<br />

example, the following code removes the second filter (the filter at index 1) from a display object:<br />

// Example of removing a filter from the middle of a stack of filters<br />

// applied to a display object named "filteredObject".<br />

var tempFilters:Array = filteredObject.filters;<br />

// Remove the second filter from the array. It's the item at index 1<br />

// because Array indexes start from 0.<br />

// The first "1" indicates the index of the filter to remove; the<br />

// second "1" indicates how many elem<strong>en</strong>ts to remove.<br />

tempFilters.splice(1, 1);<br />

// Apply the new set of filters to the display object.<br />

filteredObject.filters = tempFilters;<br />

Determining a filter’s index<br />

You need to know which filter to remove from the array, so that you know the index of the filter. You must either know<br />

(by virtue of the way the application is designed), or calculate the index of the filter to remove.<br />

The best approach is to design your application so that the filter you want to remove is always in the same position in<br />

the set of filters. For example, if you have a single display object with a convolution filter and a drop-shadow filter<br />

applied to it (in that order), and you want to remove the drop-shadow filter but keep the convolution filter, the filter<br />

is in a known position (the top-most filter) so that you can know ahead of time which Array method to use (in this<br />

case Array.pop() to remove the drop-shadow filter).<br />

If the filter you want to remove is always a certain type, but not necessarily always in the same position in the set of<br />

filters, you can check the data type of each filter in the array to determine which one to remove. For example, the<br />

following code determines which of a set of filters is a glow filter, and removes that filter from the set.<br />

Last updated 6/6/2012<br />

272

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

Saved successfully!

Ooh no, something went wrong!