This page is optimized for mobile devices, if you would prefer the desktop version just click here

0.9 Creating custom effects  (Page 4/11)

The constructor for the class named CustomEffect

The constructor is shown in its entirety in Listing 3.

The constructor for the class named customeffect.

public function CustomEffect(target:Object=null){ super(target);instanceClass = CustomEffectInstance; } //end constructor

The incoming parameter

The incoming parameter for the constructor is the generic type Object . This parameter must specify the component on which the effect is to be played.

If no target is passed as a parameter to the constructor, the default null value prevails and the target property of the object must be set. As you will see later, an alternative property named targets can be set to cause the effect to be played on multiple targets at the same time.

Call the superclass constructor

Without attempting to explain why, I am going to tell you that it is frequently necessary in OOP to cause the constructor for a class to make a callto the constructor of its superclass as the first statement in the constructor. This constructor is no exception to that rule.

The first statement in Listing 3 is a call to the constructor for the Effect class passing a reference to the target component(s) as a parameter. When that constructor returns control, the second statement inListing 3 is executed.

The instanceClass property

This class inherits a property named instanceClass from the class named Effect . According to About creating a custom effect , the factory class that you define must set the value of this property to the name of the instance class that will be usedto play the effect.

In this program, the name of the instance class is CustomEffectInstance , as shown in Listing 3. I will explain the code in that class after I finish explaining the code in this class.

This inherited property provides the mechanism that ties the instance class to the factory class.

Override the initInstance method

Listing 4 overrides an inherited method named initInstance .

Override the initinstance method.

override protected function initInstance( instance:IEffectInstance):void{super.initInstance(instance); CustomEffectInstance(instance).theDuration =this.theDuration; CustomEffectInstance(instance).rotateAngleFrom =this.rotateAngleFrom; CustomEffectInstance(instance).rotateAngleTo =this.rotateAngleTo; CustomEffectInstance(instance).wipeShowTarget =this.wipeShowTarget; CustomEffectInstance(instance).glowColor =this.glowColor; CustomEffectInstance(instance).glowInner =this.glowInner; CustomEffectInstance(instance).glowStrength =this.glowStrength; } //end initInstance

Set the property values in the instance object

According to About creating a custom effect , the purpose of this method is to copy property values from the factory class to the instance class. Flex calls thismethod from the Effect.createInstance() method. You don't have to call it yourself, but you must prepare it to be called.

A reference to the instance object as type iEffectInstance

<< Chapter < Page Page > Chapter >>

Read also:

OpenStax, Object-oriented programming (oop) with actionscript. OpenStax CNX. Jun 04, 2010 Download for free at http://cnx.org/content/col11202/1.19
Google Play and the Google Play logo are trademarks of Google Inc.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.