See how to implement a custom color transform class in ActionScript 2.0 using object-oriented programming (OOP) instead of using a procedural timeline-based code. (From the book, Flash Hacks, by Sham Bhangal, O'Reilly Media, 2004, ISBN: 0596006454.)
Flash Hack A Custom Color Transform Class - Constructor Function Transform() (Page 4 of 5 )
Next, notice the constructor function, Transform( ), optional in Action-Script 2.0, which is used to initialize instances of the class. Our constructor accepts the target clip to be used later by other methods of the class. The class then defines several public methods that can be invoked on instances of the class, such as invert( ) and fadeToWhite( ), and private methods that are for internal use only.
Notice in particular the form of the setInterval( ) invocation. In this case, the first parameter passed is an object. We invoke setInterval( ) with the keywordthis, which represents the current object (i.e., the instance of the Transform class on which applyTransform( ) was invoked). The second parameter is the name of the method to invoke onthis, namely"transition"(which must be specified as a string). Thus, at the appropriate time, the Transform.transition( ) method is invoked on the current instance,this. Invoking a method on the current instance ensures that instance properties, such asintervalandcolorObj, are in scope within transition( ). The fourth and fifth parameters passed to setInterval( ),diffTransandduration, are passed as parameters to transition( ), when it is invoked. The transition( ) method performs the specified transform over the specified duration and clears the interval when complete.
To use the code, first instantiate an instance of the Transform class, as follows (wheremyVideo_mcis an existing movie clip whose instance name has been set in the Properties panel):
var transformer:Transform = new Transform(myVideo_mc);
Then, invoke any methods of the class on the object:
transformer.invert(3000); // Invert the colors for 3 seconds transformer.fadeToWhite(2000); // Fade to white over 2 seconds
To clean up when you are done, use:
transformer.die(); delete transformer;
If you've enjoyed what you've seen here, or to get more information, click on the "Buy the book!" graphic. Pick up a copy today!