<< Chapter < Page Chapter >> Page >

Creating a custom effect

You must define two classes to create a custom effect. One class is a factory class that extends the class named Effect . The other class is an instance class that extends the class named EffectInstance .

The instance class plays the effect

When the time comes to play the effect on a component, the factory class instantiates an object of the instance class to actually play the effect. If thesame effect is played on two or more components at the same time, a different object of the instance class is instantiated to play the effect oneach component.

Play three effects in parallel

As explained in Listing 1, the custom class that I designed for use in this lesson plays the following three effects in parallel:

  • WipeRight
  • Rotate
  • Glow

You learned about something similar to this in my earlier lesson titled Events, Triggers, and Effects . However, in that lesson I didn't combine the three effects into a single custom effect the way that I will in this lesson.

Knowledge of OOP is required

I will do the best that I can to explain this code. Even at that, you are likely to need a pretty good understanding of object-oriented programming tounderstand the code required to create a custom effect. As you will see later, the required code is steeped in overridden methods, interfaces, and otherobject-oriented concepts.

The class named CustomEffect

The class named CustomEffect begins in Listing 2. A complete listing of the class is provided in Listing 19 near the end of the lesson.

Beginning of the class named customeffect.

package CustomClasses{ import mx.effects.Effect;import mx.effects.IEffectInstance; import mx.events.EffectEvent;public class CustomEffect extends Effect{//Would prefer to make these private and use implicit// setter methods, but I decided to leave them public // to simplify the code.public var theDuration:Number = 2000;//default value public var rotateAngleFrom:Number = 0;//default valuepublic var rotateAngleTo:Number = 360;//default value public var wipeShowTarget:Boolean = true;//defaultpublic var glowColor:uint = 0xFF0000;//default value public var glowInner:Boolean = true;//default valuepublic var glowStrength:Number = 255;//default value

The factory class

Of the two required classes, this is the factory class that I mentioned earlier. This class must extend the class named Effect , and will override methods inherited from that class.

Public instance variables

Listing 2 declares and initializes seven public instance variables that will be used to set properties on the WipeRight object, the Rotate object, and the Glow object. I provided default values for these variables so that the program will work even if thedriver program fails to provide the required values.

Could use implicit setter methods

As I mentioned in the comments, I would prefer to make these variables private and provide an implicit setter method for each variable. However, Idecided to make them public to simplify the code and make it easier to explain.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  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.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?

Ask