# Managing filters (applying and removing randomly)

**URL:** <https://forum.kirupa.com/t/managing-filters-applying-and-removing-randomly/292049>\
**Category:** flash\
**Created:** [July 9, 2009, 12:45pm UTC](https://forum.kirupa.com/t/managing-filters-applying-and-removing-randomly/292049 "2009-07-09T12:45:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![RUBn](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@RUBn](https://forum.kirupa.com/u/RUBn)\
**Post date:** [July 9, 2009, 12:45pm UTC](https://forum.kirupa.com/t/managing-filters-applying-and-removing-randomly/292049/1 "2009-07-09T12:45:20Z")

</div>

Hi guys,

Here is some code I thought you guys would like.  
Ever had trouble with adding and removing different filters to a display object and bumping into that wall with that annoying filters copy behavior?

Here is a nice way to get rid of all your troubles.

[AS]  
var filterDic=new Dictionary();  
DisplayObject.prototype.applyFilter = function (filter) {  
if (!filterDic[this]) filterDic[this]=this.filters;  
filterDic[this].push(filter); trace(“apply”+(filterDic[this].length-1))  
this.filters=filterDic[this];  
}  
DisplayObject.prototype.removeFilter = function (filter) { trace(filterDic[this].indexOf(filter));  
filterDic[this].splice(filterDic[this].indexOf(filter), 1);  
this.filters=filterDic[this];  
}

//Example use  
var glowFilter = new GlowFilter(0x00FFFF, 1.0, 8, 8, 6, 2);  
var redFilter = new ColorMatrixFilter([1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]);  
DO.applyFilter (glowFilter);  
DO.applyFilter (redFilter );  
DO.removeFilter (glowFilter); //red stays!![/AS]

I didn’t write it in a class, since it involves prototype syntax and I don’t know if that’s adviced in a class. (Isn’t there a include statement or can “import” also include non-classes?).
