Example: Passing parameters to effects
To make your effects more robust, you often design them to let the user pass parameters to
them. The example in this section modifies the sound effect from the previous section to take
a parameter that specifies the MP3 file to play:
package myEffects
{
// MySoundParam.as
import mx.effects.Effect;
import mx.effects.EffectInstance;
import mx.effects.IEffectInstance;
public class MySoundParam extends Effect
{
// Define a variable for the MP3 URL
// and give it a default value.
public var soundMP3:String=
"http://localhost:8100/flex/assets/default.mp3";
// Define constructor with optional argument.
public function MySoundParam(targetObj:Object = null) {
// Call base class constructor.
super(targetObj);
// Set instanceClass to the name of the effect instance class.
instanceClass= MySoundParamInstance;
}
// Override getAffectedProperties() method to return an empty array.
override public function getAffectedProperties():Array {
return [];
}
// Override initInstance() method.
override protected function initInstance(inst:IEffectInstance):void {
super.initInstance(inst);
// initialize the corresponding parameter in the instance class.
MySoundParamInstance(inst).soundMP3 = soundMP3;
}
}
}
In the MySoundParam class, you define a variable named soundMP3 that enables the user of
the effect to specify the URL of the MP3 file to play. You also modify your override of the
method to pass the value of the soundMP3 variable to the instance class.
initInstance()
About creating a custom effect
235