Using AS3 to Add & Remove Filters

Posted by docaberle 
Using AS3 to Add & Remove Filters
January 27, 2011 09:42PM
I'm extremely confused at why the code below will work fine for the flag filter but only works once for the glitter filter. I'm trying to show different filter effects so users can choose which effect they want in their app. I have different images to apply these filters to as well but that's easy if I can get this working. Simple setup. A movie clip named myImage with a jpg inside. Two buttons. I also don't understand why I have to addFilter and set the target on line 11 & 12. If I don't the same lines in the function return errors.

import com.jumpeye.Events.FLASHEFFEvents;
import com.jumpeye.flashEff2.filter.flag.FEFFlag;
import com.jumpeye.flashEff2.filter.glittering.FEFGlittering;

var effect:FlashEff2Code = new FlashEff2Code();
addChildAt(effect, 0);	//Needed to get the buttons on top 

var flag:FEFFlag = new FEFFlag();
var glittering:FEFGlittering = new FEFGlittering();

effect.addFilter(flag);
effect._targetInstanceName = "myImage";

flagBtn.addEventListener(MouseEvent.CLICK, flagClickBtnHandler); 
glitterBtn.addEventListener(MouseEvent.CLICK, glitterClickBtnHandler); 
 
function flagClickBtnHandler(event:MouseEvent):void { 
	if (effect.filterList[0] != null) {	//Remove filters if there are any
		effect.removeAllFilters();
	}
	effect.addFilter(flag);
	effect._targetInstanceName = "myImage";
	trace("Flag Effect Enabled");
}
function glitterClickBtnHandler(event:MouseEvent):void { 
	if (effect.filterList[0] != null) {	//Remove filters if there are any
		effect.removeAllFilters();
	}
	effect.addFilter(glittering);
	effect._targetInstanceName = "myImage";
	trace("Glitter Effect Enabled");
}

Re: Using AS3 to Add & Remove Filters
January 28, 2011 12:09PM
You don't have to set the same target twice. In your case is enough to set the component at the beginning.
Regarding the filter problem, it seems that there is a problem with the glittering effect. We will try to fix the problem as soon as have time. Thank you for understanding and we apologize for any inconvenience.

However, a workaround for this issue is to add the glittering filter each time you click on the glitterBtn.
Here is the code:
import com.jumpeye.Events.FLASHEFFEvents;
import com.jumpeye.flashEff2.filter.flag.FEFFlag;
import com.jumpeye.flashEff2.filter.glittering.FEFGlittering;

var effect:FlashEff2Code = new FlashEff2Code();
addChildAt(effect, 0);
effect._targetInstanceName = "myImage";

var flag:FEFFlag = new FEFFlag();
var glow:FEFGlow = new FEFGlow();

effect.addFilter(flag);

flagBtn.addEventListener(MouseEvent.CLICK, flagClickBtnHandler);
glitterBtn.addEventListener(MouseEvent.CLICK, glitterClickBtnHandler);

function flagClickBtnHandler(event:MouseEvent):void {

effect.removeAllFilters();
effect.addFilter(flag);

}
function glitterClickBtnHandler(event:MouseEvent):void {

effect.removeAllFilters();
var glittering:FEFGlittering = new FEFGlittering();
effect.addFilter(glittering);

}



Edited 1 time(s). Last edit at 01/28/2011 12:14PM by kneo.
Sorry, you do not have permission to post/reply in this forum.