您的位置:首页 > 其它

[ActionScript 3.0] 创建倒影

2015-06-19 17:14 351 查看


package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.GradientType;
import flash.display.Loader;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.net.URLRequest;

/**
* ...
* @author Frost.Yen
*/
[SWF(width=800, height=1000, backgroundColor=0x000000, frameRate=30)]
public class Reflection extends Sprite
{
private var ldr:Loader=new Loader();
private var pic:Sprite=new Sprite();
public function Reflection()
{
ldr.load(new URLRequest("http://hiphotos.baidu.com/frostyen/pic/item/7e49d8b53f6b48ffd9335aa2.jpg"));
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
}

private function onImageLoaded(e:Event):void
{
this.addChild(pic);
pic.addChild(ldr);
pic.x = 200;
pic.y = 10;
ldr.scaleX = ldr.scaleY = .5;
createRef(pic,5,0);
}
/**
*
* @param obj  需创建倒影的对象
* @param distance  对象与倒影的距离
* @param offset  倒影的偏移量
*/
private function createRef(obj:DisplayObject,distance:Number=5,offset:Number=0):void
{
// 倒置
var bd:BitmapData = new BitmapData(obj.width,obj.height,true);
var mtx:Matrix = new Matrix();
mtx.d = -1;
mtx.ty = bd.height;
bd.draw(obj, mtx);

// 添加倒影渐变
var _width:int = bd.width;
var _height:int = bd.height;

mtx.createGradientBox(_width, _height, 0.5 * Math.PI,0,offset );
var shape:Shape = new Shape();
shape.graphics.beginGradientFill(GradientType.LINEAR, [0,0,0], [0.6,0.1, 0], [0,200, 255], mtx);
shape.graphics.drawRect(0, 0, _width, _height);
shape.graphics.endFill();
var mask_bd:BitmapData = new BitmapData(_width,_height,true,0);
mask_bd.draw(shape);
// 生成最终效果
bd.copyPixels(bd, bd.rect, new Point(0, 0), mask_bd, new Point(0, 0), false);
// 将倒影放置于对象下方
var ref:Bitmap = new Bitmap(bd);
ref.y = obj.height + obj.y + distance;
ref.x = obj.x;
obj.parent.addChild(ref);
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: