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

private var _filterFactory:IFilterFactory;<br />

public function setFilter(factory:IFilterFactory):void<br />

{<br />

...<br />

}<br />

_filterFactory = factory;<br />

_filterFactory.addEv<strong>en</strong>tList<strong>en</strong>er(Ev<strong>en</strong>t.CHANGE, filterChange);<br />

Notice that, as described previously, the controller instance doesn’t know the specific data type of the filter factory<br />

instance that it is giv<strong>en</strong>; it only knows that the object implem<strong>en</strong>ts the IFilterFactory instance, meaning it has a<br />

getFilter() method and it dispatches a change (Ev<strong>en</strong>t.CHANGE) ev<strong>en</strong>t wh<strong>en</strong> the filter changes.<br />

Wh<strong>en</strong> the user changes a filter’s properties in the filter’s panel, the controller instance finds out that the filter has<br />

changed through the filter factory’s change ev<strong>en</strong>t, which calls the controller instance’s filterChange() method. That<br />

method, in turn, calls the applyTemporaryFilter() method:<br />

private function filterChange(ev<strong>en</strong>t:Ev<strong>en</strong>t):void<br />

{<br />

applyTemporaryFilter();<br />

}<br />

private function applyTemporaryFilter():void<br />

{<br />

var curr<strong>en</strong>tFilter:BitmapFilter = _filterFactory.getFilter();<br />

}<br />

// Add the curr<strong>en</strong>t filter to the set temporarily<br />

_curr<strong>en</strong>tFilters.push(curr<strong>en</strong>tFilter);<br />

// Refresh the filter set of the filter target<br />

_curr<strong>en</strong>tTarget.filters = _curr<strong>en</strong>tFilters;<br />

// Remove the curr<strong>en</strong>t filter from the set<br />

// (This doesn't remove it from the filter target, since<br />

// the target uses a copy of the filters array internally.)<br />

_curr<strong>en</strong>tFilters.pop();<br />

The work of applying the filter to the display object occurs within the applyTemporaryFilter() method. First, the<br />

controller retrieves a refer<strong>en</strong>ce to the filter object by calling the filter factory’s getFilter() method.<br />

var curr<strong>en</strong>tFilter:BitmapFilter = _filterFactory.getFilter();<br />

The controller instance has an Array instance variable named _curr<strong>en</strong>tFilters, in which it stores all the filters that<br />

have be<strong>en</strong> applied to the display object. The next step is to add the newly updated filter to that array:<br />

_curr<strong>en</strong>tFilters.push(curr<strong>en</strong>tFilter);<br />

Next, the code assigns the array of filters to the display object’s filters property, which actually applies the filters to<br />

the image:<br />

_curr<strong>en</strong>tTarget.filters = _curr<strong>en</strong>tFilters;<br />

Finally, since this most rec<strong>en</strong>tly added filter is still the “working” filter, it shouldn’t be perman<strong>en</strong>tly applied to the<br />

display object, so it is removed from the _curr<strong>en</strong>tFilters array:<br />

Last updated 6/6/2012<br />

298

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

Saved successfully!

Ooh no, something went wrong!