Flash Hack A Custom Color Transform Class - Code Listing: A Custom Transform Class (
Page 2 of 5 )
Although we can’t give a full course on OOP and ActionScript 2.0 here, this custom color transform class can be used even if you don’t understand OOP. And we’ll examine several aspects of the code after the code listing.
Here is our object-oriented version, implemented as a custom Transform class, which must be stored in an external Transform.as file:
// This ActionScript 2.0 code must go in an external Transform.as file
class Transform {
// NEG_TRANS inverts the color values.
// NEUTRAL_TRANS resets the color values.
// BLACK_TRANS sets the color values to black.
// WHITE_TRANS sets the color values to white.
// RATE sets the rate the effects will run at in ms.
private static var NEG_TRANS:Object = {ra:-100, rb:255,
ga:-100, gb:255, ba:-100, bb:255, aa:100, ab:0};
private static var NEUTRAL_TRANS:Object = {ra:100, rb:0,
ga:100, gb:0, ba:100, bb:0, aa:100, ab:0};
private static var BLACK_TRANS:Object = {ra:100, rb:-255,
ga:100, gb:-255, ba:100, bb:-255, aa:100, ab:0};
private static var WHITE_TRANS:Object = {ra:100, rb:255,
ga:100, gb:255, ba:100, bb:255, aa:100, ab:0};
private static var RATE:Number = 50;
private var interval:Number;
private var startTime:Number;
private var colorObj:Color;
// Constructor accepts target clip to which to apply transforms
public function Transform(targetClip:MovieClip) {
colorObj = new Color(targetClip);
}
// Inverts the color values
public function invert(duration:Number):Void {
applyTransform(NEG_TRANS, duration);
}
// Resets the color to the default values set in the authoring tool
public function reset(duration:Number):Void {
applyTransform(NEUTRAL_TRANS, duration);
}
// Performs a fade to black over specified duration in ms
public function fadeToBlack(duration:Number):Void {
applyTransform(BLACK_TRANS, duration);
}
// Performs a fade to white over specified duration in ms
public function fadeToWhite(duration:Number):Void {
applyTransform(WHITE_TRANS, duration);
}
// Function to initiate a fade and set up an interval to
// complete it over time.
private function applyTransform(transObject:Object,
duration:Number):Void {
var getTrans:Object = colorObj.getTransform( );
var diffTrans:Object = new Object( );
startTime = getTimer( );
for (var i in transObject) {
diffTrans[i] = (transObject[i] - getTrans[i]) / (duration / RATE);
}
// Use the form of setInterval( ) that invokes a method of an object,
// so that instance properties are in scope (the object is this).
// First parameter is the object (this) on which to invoke the
// method specified by the second parameter (in this case
// "transition", which must be passed as a string).
// Third parameter is interval duration in ms.
// Fourth, fifth, and sixth parameters get passed to transition( )
interval = setInterval(this, "transition", RATE, transObject, diffTrans,
duration);
}
// This method applies each step of the color transformation.
private function transition(transObject:Object, diffTrans:Object,
duration:Number):Void {
var getTrans:Object = colorObj.getTransform( );
for (var i in diffTrans) {
getTrans[i] += diffTrans[i];
}
colorObj.setTransform(getTrans);
if (getTimer( ) - startTime > duration) {
// Complete the final step in the transition
colorObj.setTransform(transObject);
// Clear the interval to stop the effect
clearInterval(interval);
}
// Force the screen to refresh between frames
updateAfterEvent( );
}
public function die( ):Void {
// Perform any cleanup code here
}
}
 |
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!
Visit the O'Reilly Network http://www.oreillynet.com for more online content. |