topical media & game development
student-twitter-flock-AnimatedImage.ax
student-twitter-flock-AnimatedImage.ax
(swf
)
[ flash
]
flex
package {
import flash.filters.ColorMatrixFilter;
import flash.utils.setInterval;
import flash.utils.setTimeout;
import mx.controls.Image;
public class @ax-student-twitter-flock-AnimatedImage {
private var image:Image;
private var unselectedImages:Array, selectedImages:Array;
private var colorRed:Number, colorGreen:Number, colorBlue:Number;
private var selected:Boolean = false;
private var counter:int;
private var anim:Array;
public function @ax-student-twitter-flock-AnimatedImage(image:Image, anim:Array) {
// Starts the animation at a random time so that the boids won't all
// flap their wings at the same time.
this.image = image;
this.anim = anim;
this.colorRed = this.colorGreen = this.colorBlue = 0.5;
setTimeout(startAnimation, Math.ceil(Math.random() * 300));
}
public function setColor (color:Array):void {
// Sets the color of the filter to the given color.
this.colorRed = color[0];
this.colorGreen = color[1];
this.colorBlue = color[2];
resetColor();
}
private function startAnimation ():void {
// Starts the animation.
this.counter = 0;
setInterval(animateImage, 150);
}
private function animateImage ():void {
this.image.source = anim[counter];
counter++;
if (counter > 3) counter = 0;
}
private function makeGrayscale (): void {
// Filters the Image to greyscale.
this.image.filters = []
var RedLum: Number = 0.2225;
var GreenLum: Number = 0.7169;
var BlueLum: Number = 0.0606;
// channel Red, Green, Blue and Alpha, transforming the image in black and white
var bwMatrix: Array = [ RedLum, GreenLum, BlueLum, 0, 0,
RedLum, GreenLum, BlueLum, 0, 0,
RedLum, GreenLum, BlueLum, 0, 0,
0, 0, 0, 1, 0];
var filter:ColorMatrixFilter = new ColorMatrixFilter (bwMatrix);
this.image.filters = [filter];
}
private function makeRed (): void {
// Filters the image to become red for when the boid is selected.
this.image.filters = []
var RedLum: Number = 0.2225;
var GreenLum: Number = 0.7169;
var BlueLum: Number = 0.0606;
// channel Red, Green, Blue and Alpha
var bwMatrix: Array = [ 1, 1, 1, 0, 0,
RedLum, GreenLum, BlueLum, 0, 0,
RedLum, GreenLum, BlueLum, 0, 0,
0, 0, 0, 1, 0];
var filter:ColorMatrixFilter = new ColorMatrixFilter (bwMatrix);
this.image.filters = [filter];
}
private function resetColor (): void {
// Filters the image back to the color that the boid was initialised with.
this.image.filters = []
var RedLum: Number = this.colorRed;
var GreenLum: Number = this.colorGreen;
var BlueLum: Number = this.colorBlue;
// channel Red, Green, Blue and Alpha
var bwMatrix: Array = [ RedLum, 0, 0, 0, 0,
0, GreenLum, 0, 0, 0,
0, 0, BlueLum, 0, 0,
0, 0, 0, 1, 0];
var filter:ColorMatrixFilter = new ColorMatrixFilter (bwMatrix);
this.image.filters = [filter];
}
public function move(x:int, y:int):void {
this.image.x = x - 15;
this.image.y = y - 23;
}
public function rotate(rotation:Number):void {
this.image.rotation = rotation;
}
public function select ():void {
this.selected = true;
makeRed();
}
public function deselect ():void {
this.selected = false;
resetColor();
}
public function getImage ():Image {
return this.image;
}
}
}
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.