要在FLASH中畫出倒影事件不難但也不容易的事情
不過對岸網友分享了一個CLASS
只用import進來就可以使用了
import Reflect; var re:Reflect=new Reflect(mc,100,255, 0,-1,0);//圖片用(不做動態更新) var re2:Reflect=new Reflect(mc2,100,255, 0,10,0);//影片用 |
有了這個CLASS要做倒影效果就方便很多了…
CLASS如下:
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | package { import flash.events.*; import flash.utils.Timer; import flash.display.MovieClip; import flash.display.DisplayObject; import flash.display.BitmapData; import flash.display.Bitmap; import flash.geom.Matrix; import flash.display.GradientType; import flash.display.SpreadMethod; public class Reflect extends MovieClip { //static var for the version of this class private static var VERSION:String = "1.0"; //reference to the movie clip we are reflecting private var mc:MovieClip; //the BitmapData object that will hold a visual copy of the mc private var mcBMP:BitmapData; //the BitmapData object that will hold the reflected image private var reflectionBMP:Bitmap; //the clip that will act as out gradient mask private var gradientMask_mc:MovieClip; //how often the reflection should update (if it is video or animated) private var timer1:Timer; //the size the reflection is allowed to reflect within private var bounds:Object; //the distance the reflection is vertically from the mc private var distance:Number = 0; private var alpha1:Number; private var ratio:Number ; private var updateTime:Number; private var reflectionDropoff:Number; private var matr:Matrix; private var reflectionBMPRef:DisplayObject; private var gradientMaskRef:DisplayObject; /*TODO:constructor *parameters: * -ref_mc:MovieClip(An instance of MovieClip which is to be reflected.) * -alpha:int(The alpha level of the reflection clip.The value must between 0-100.) * -ratio:int(The ratio opaque color used in the gradient mask.The value must between 0-255.) * -distance:int(The distance the reflection starts from the bottom of the mc.) * -updateTime:int(The update time interval.) * -reflectionDropoff:int(The distance at which the reflection visually drops off at): */ public function Reflect(ref_mc:MovieClip, alpha:int=50, ratio:int=50, distance:int=0, updateTime:int=0, reflectionDropoff:int=1) { //the clip being reflected mc =ref_mc; //the alpha level of the reflection clip this.alpha1 = alpha/100; //the ratio opaque color used in the gradient mask this.ratio = ratio; //update time interval this.updateTime = updateTime; //the distance at which the reflection visually drops off at this.reflectionDropoff = reflectionDropoff; //the distance the reflection starts from the bottom of the mc this.distance = distance; //store width and height of the clip var mcHeight = mc.height; var mcWidth = mc.width; //store the bounds of the reflection bounds = new Object(); bounds.width = mcWidth; bounds.height = mcHeight; //create the BitmapData that will hold a snapshot of the movie clip mcBMP = new BitmapData(bounds.width, bounds.height, true, 0xFFFFFF); mcBMP.draw(mc); //create the BitmapData the will hold the reflection reflectionBMP = new Bitmap(mcBMP); //flip the reflection upside down reflectionBMP.scaleY = -1; //move the reflection to the bottom of the movie clip reflectionBMP.y = (bounds.height*2) + distance; //add the reflection to the movie clip's Display Stack reflectionBMPRef = mc.addChild(reflectionBMP); reflectionBMPRef.name = "reflectionBMP"; //add a blank movie clip to hold our gradient mask gradientMaskRef = mc.addChild(new MovieClip()); gradientMaskRef.name = "gradientMask_mc"; //get a reference to the movie clip - cast the DisplayObject that is returned as a MovieClip gradientMask_mc = mc.getChildByName("gradientMask_mc") as MovieClip; //set the values for the gradient fill var fillType:String = GradientType.LINEAR; var colors:Array = [0xFFFFFF, 0xFFFFFF]; var alphas:Array = [alpha1, 0]; var ratios:Array = [0, ratio]; var spreadMethod:String = SpreadMethod.PAD; //create the Matrix and create the gradient box matr = new Matrix(); //set the height of the Matrix used for the gradient mask var matrixHeight:Number; if (reflectionDropoff<=0) { matrixHeight = bounds.height; } else { matrixHeight = bounds.height/reflectionDropoff; } matr.createGradientBox(bounds.width, matrixHeight, Math.PI/2, 0, 0); //create the gradient fill gradientMask_mc.graphics.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod); gradientMask_mc.graphics.drawRect(0,0,bounds.width,bounds.height); //position the mask over the reflection clip gradientMask_mc.y = mc.getChildByName("reflectionBMP").y - mc.getChildByName("reflectionBMP").height; //cache clip as a bitmap so that the gradient mask will function gradientMask_mc.cacheAsBitmap = true; mc.getChildByName("reflectionBMP").cacheAsBitmap = true; //set the mask for the reflection as the gradient mask mc.getChildByName("reflectionBMP").mask = gradientMask_mc; //if we are updating the reflection for a video or animation do so here if(updateTime > -1) { timer1 = new Timer(updateTime,0); timer1.addEventListener(TimerEvent.TIMER,timer1_c); timer1.start(); } } private function timer1_c(event:TimerEvent):void{ update(mc); } private function update(mc):void { //updates the reflection to visually match the movie clip mcBMP = new BitmapData(bounds.width, bounds.height, true, 0xFFFFFF); mcBMP.draw(mc); reflectionBMP.bitmapData = mcBMP; } public function setBounds(w:int,h:int):void{ //allows the user to set the area that the reflection is allowed //this is useful for clips that move within themselves bounds.width = w; bounds.height = h; gradientMask_mc.width = bounds.width; redrawBMP(mc); } public function setDistance(d:int):void{ //allows the user to set the distance distance = d; reflectionBMP.y = (bounds.height*2) + distance; gradientMask_mc.y = mc.getChildByName("reflectionBMP").y - mc.getChildByName("reflectionBMP").height; } public function setAlpha(a:int):void{ alpha1 = a/100; gradientMask_mc.graphics.clear(); gradientMask_mc.graphics.beginGradientFill(GradientType.LINEAR, [0xFFFFFF, 0xFFFFFF], [alpha1, 0], [0, ratio], matr, SpreadMethod.PAD); gradientMask_mc.graphics.drawRect(0,0,bounds.width,bounds.height); gradientMask_mc.cacheAsBitmap = true; } public function setRatio(r:int):void{ ratio = r; gradientMask_mc.graphics.clear(); gradientMask_mc.graphics.beginGradientFill(GradientType.LINEAR, [0xFFFFFF, 0xFFFFFF], [alpha1, 0], [0, ratio], matr, SpreadMethod.PAD); gradientMask_mc.graphics.drawRect(0,0,bounds.width,bounds.height); gradientMask_mc.cacheAsBitmap = true; } public function setUpdateTime(t:int):void { updateTime=t; timer1.delay=t; } public function setReflectionDropoff(r:int):void { reflectionDropoff=r; var matrixHeight:Number; if (reflectionDropoff<=0) { matrixHeight = bounds.height; } else { matrixHeight = bounds.height/reflectionDropoff; } matr.createGradientBox(bounds.width, matrixHeight, Math.PI/2, 0, 0); gradientMask_mc.graphics.clear(); gradientMask_mc.graphics.beginGradientFill(GradientType.LINEAR, [0xFFFFFF, 0xFFFFFF], [alpha1, 0], [0, ratio], matr, SpreadMethod.PAD); gradientMask_mc.graphics.drawRect(0,0,bounds.width,bounds.height); gradientMask_mc.cacheAsBitmap = true; } public function redrawBMP(mc:MovieClip):void { // redraws the bitmap reflection mcBMP.dispose(); mcBMP = new BitmapData(bounds.width, bounds.height, true, 0xFFFFFF); mcBMP.draw(mc); } public function destroy():void{ //provides a method to remove the reflection mc.removeChild(mc.getChildByName("reflectionBMP")); reflectionBMP = null; mcBMP.dispose(); timer1.stop(); mc.removeChild(mc.getChildByName("gradientMask_mc")); } } } |
另外還有比較進階的用法可以看另一篇
flash比我當年接觸時複雜許多!@@
也比我當年剛接觸時複雜許多…
不過…能做的實情也真的多很多阿~~