[AS3.0] Reflection Class

Been working on a project with lost of reflections. I made this class to help me create reflections…
Reflection_demo

Usage:
var refl :Reflection = new Reflection( your_clip_to_be_reflected,
reflection_height,
reflection_strenght,
reflection_yOffset ) ;

addChild( refl );


Reflection class:

View CodeACTIONSCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package
{
	import flash.display.*;
	import flash.geom.*;
 
	public class Reflection extends Sprite
	{
		//publics:
		public var reflectedClip      :DisplayObjectContainer;
		public var reflectionHeight   :Number;
		public var reflectionStrength :Number;
		public var reflectionOffsetY  :Number;
 
		//privates:
		private var reflectionHolder  :Sprite;
		private var bm_reflection     :Bitmap;
		private var refMask           :Sprite;
 
		//////////////////////////////////////////////
		//               CONSTRUCTOR:
		//////////////////////////////////////////////
		public function Reflection( reflectedClip:DisplayObjectContainer, height:Number = 255, strength:Number = 1, reflectionOffsetY:Number = 1 )
		{
			this.reflectedClip       = reflectedClip;
			this.reflectionHeight    = height;
			this.reflectionStrength  = strength;
			this.reflectionOffsetY   = reflectionOffsetY;
			build();
		}
 
		//////////////////////////////////////////////
		//               BUILDER FUNCTIONS:
		//////////////////////////////////////////////
		private function build():void
		{
			buildReflection ();
			attachItems     ();
			positionElements();
		}
 
		private function buildReflection():void
		{
			reflectionHolder = new Sprite();
 
			var bmd:BitmapData = new BitmapData( reflectedClip.width, reflectedClip.height, true, 0xc61916 );
			bmd.draw( reflectedClip );
 
			bm_reflection             = new Bitmap( bmd );
			bm_reflection.y           = bm_reflection.height;
			bm_reflection.x           = 0;
			bm_reflection.rotation    = 180;
			bm_reflection.scaleX      = -1;
			bm_reflection.alpha       = reflectionStrength;
 
			refMask                   = new Sprite();
			var fillType      :String = GradientType.LINEAR;
			var colors        :Array  = [0xFFFFFF, 0x0000FF];
			var alphas        :Array  = [100, 0];
			var ratios        :Array  = [0, reflectionHeight];
			var matr          :Matrix = new Matrix();
			var spreadMethod  :String = SpreadMethod.PAD;
 
			matr.createGradientBox(reflectedClip.height * .9, reflectedClip.width, 0,0, 0);
			refMask.graphics.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod);
			refMask.graphics.drawRect(0, 0, reflectedClip.height, reflectedClip.width);
 
			refMask.y                        = 0;
			refMask.x                        = bm_reflection.width
			refMask.rotation                 = 90;
 
			bm_reflection.cacheAsBitmap      = true;
			refMask.cacheAsBitmap            = true;
			bm_reflection.mask               = refMask;
		}
 
		//////////////////////////////////////////////
		//        ATTACHER & POSITION FUNCTIONS:
		//////////////////////////////////////////////
		private function attachItems():void
		{
			this.addChild( reflectionHolder );
			reflectionHolder.addChild( bm_reflection );
			reflectionHolder.addChild( refMask );
		}
 
		private function positionElements():void
		{
			this.x = reflectedClip.x;
			this.y = reflectedClip.y + reflectedClip.height + reflectionOffsetY;
		}
 
		//////////////////////////////////////////////
		//               KILL FUNCTION:
		//////////////////////////////////////////////
		public function kill():void
		{
			reflectionHolder.removeChild( refMask );
			reflectionHolder.removeChild( bm_reflection );
			this.removeChild( reflectionHolder );
		}
	}
}

Have fun with it!
Rackdoll

Comments are closed.


Categories
Archives
LinkedIn
R. Kollau