User Guide
232 Creating Effects
Example: Defining a simple effect
To define a simple custom effect, you create a factory class from the Effect base class, and the
instance class from the mx.effects.EffectInstance class. The following example shows an effect
class that uses a Sound object to play an embedded MP3 file when a user action occurs. This
example is a simplified version of the SoundEffect class that ships with Flex.
package myEffects
{
// myEffects/MySound.as
import mx.effects.Effect;
import mx.effects.EffectInstance;
import mx.effects.IEffectInstance;
public class MySound extends Effect
{
// Define constructor with optional argument.
public function MySound(targetObj:Object = null) {
// Call base class constructor.
super(targetObj);
// Set instanceClass to the name of the effect instance class.
instanceClass= MySoundInstance;
}
// This effect modifies no properties, so your
// override of getAffectedProperties() method
// returns an empty array.
override public function getAffectedProperties():Array {
return [];
}
// Override initInstance() method.
override protected function initInstance(inst:IEffectInstance):void
{
super.initInstance(inst);
}
}
}
The package statement in your class specifies that you should deploy it in a directory called
myEffects. In this example, you place it in the subdirectory of the directory that contains your
Flex application. Therefore, the namespace definition in your Flex application is
xmlns:MyComp="myEffects.*". For more information on deployment, see Chapter 6,
“Compiling Components,” on page 63.