topical media & game development
#graphic-flex-image-effects-01-Flex-DrawingGradientFills.ax
#graphic-flex-image-effects-01-Flex-DrawingGradientFills.ax
[swf]
[flash]
flex
package {
import flash.display.GradientType;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Matrix;
[SWF(width=550, height=400, backgroundColor=0xFFFFFF)]
Demonstrates the drawing of a radial gradient fill that updates as the mouse moves.
public class @ax-graphic-flex-image-effects-01-Flex-DrawingGradientFills extends Sprite {
Constructor. This establishes the MOUSE_MOVE listener and draws the background initially.
public function @ax-graphic-flex-image-effects-01-Flex-DrawingGradientFills() {
stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove);
drawBackground();
}
Draws the background with a radial gradient.
private function drawBackground():void {
var colors:Array = [0xFFFF00, 0xFF0000, 0x000000];
var alphas:Array = [1, 1, 1];
var ratios:Array = [50, 100, 255];
var matrix:Matrix = new Matrix();
// the offset of the gradient, which moves its center, is determined by the mouse position
matrix.createGradientBox(200, 200, 0, stage.mouseX-100, stage.mouseY-100);
graphics.clear();
graphics.beginGradientFill(GradientType.RADIAL, colors, alphas, ratios, matrix);
graphics.lineTo(stage.stageWidth, 0);
graphics.lineTo(stage.stageWidth, stage.stageHeight);
graphics.lineTo(0, stage.stageHeight);
graphics.lineTo(0, 0);
graphics.endFill();
}
Handler for when the mouse is moved. This redraws the gradient.
parameter: event The MouseEvent caused by the movement.
private function onStageMouseMove(event:MouseEvent):void {
drawBackground();
event.updateAfterEvent();
}
}
}
(C) Æliens
04/09/2009
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.