做一个小例子演示滤镜的动态效果
在舞台建两个元件或导入两张图片到舞台再转化为元件,分别取实例名apple01和apple02;
var blurSpeed:Number = 1;//首先使用动画滤镜
var bevelSpeed:Number = 5;
//为两个苹果添加MOUSE_OVER 事件侦听
apple01.addEventListener(MouseEvent.MOUSE_OVER,mouseOverApple01);
apple02.addEventListener(MouseEvent.MOUSE_OVER, mouseOverApple02);
//这两个苹果添加MOUSE_OUT 事件侦听 ;
apple01.addEventListener(MouseEvent.MOUSE_OUT,mouseOutApple01);
apple02.addEventListener(MouseEvent.MOUSE_OUT, mouseOutApple02);
/*为两个苹果添加ENTER_FRAME,这样我们就能每隔一帧的时间绘制它们*/
apple01.addEventListener(Event.ENTER_FRAME,enterFrameApple01);
apple02.addEventListener(Event.ENTER_FRAME, enterFrameApple02);
//创建并附加模糊滤镜给apple01 ;
var blur:BlurFilter = new BlurFilter();
blur.blurX = 20;
blur.blurY = 20;
blur.quality = BitmapFilterQuality.HIGH;
apple01.filters = [blur];
//创建一个斜角滤镜给apple02
var bevelFilter:BevelFilter = new BevelFilter(10,45,0x000000,1,0xffffff,1,0,0,0,BitmapFilterQuality.HIGH,BitmapFilterType.INNER,false);
apple02.filters = [bevelFilter];
//设置省缺值(鼠标没移动到苹果上时)
var mouseIsOverApple01:Boolean = false;
var mouseIsOverApple02:Boolean = false;
//当鼠标移到apple01上时调用
function mouseOverApple01(event:MouseEvent):void
{
mouseIsOverApple01 = true;
}
//当鼠标移到apple02上时调用
function mouseOverApple02(event:MouseEvent):void
{
mouseIsOverApple02 = true;
}
//当鼠标移出apple01 时调用
function mouseOutApple01(event:MouseEvent):void
{
mouseIsOverApple01 = false;
}
//当鼠标移出apple02 时调用
function mouseOutApple02(event:MouseEvent):void
{
mouseIsOverApple02 = false;
}
//这个函数绘制apple01 的动画
function enterFrameApple01(event:Event):void
{
//如果鼠标在苹果上移动减少模糊
if (mouseIsOverApple01 == true)
{
blur.blurX -= blurSpeed;
blur.blurY -= blurSpeed;
}
/* 如果鼠标移出苹果,并且模糊没超过20,我们增加模糊。*/
if (mouseIsOverApple01 == false && blur.blurX <= 20)
{
blur.blurX += blurSpeed;
blur.blurY += blurSpeed;
}
/*在改变模糊滤镜后我需要重新分配滤镜*/
apple01.filters = [blur];
}
//这个函数绘制apple02的动画
function enterFrameApple02(event:Event):void
{
//如果鼠标移到这个苹果上,我们增加模糊滤镜直到100
if (mouseIsOverApple02 == true && bevelFilter.blurX < 100)
{
bevelFilter.blurX += bevelSpeed;
bevelFilter.blurY += bevelSpeed;
//我们需要分配一个力度给斜角让它可见
bevelFilter.strength = 5;
}
//如果鼠标移出apple02, 减少模糊
if (mouseIsOverApple02 == false)
{
bevelFilter.blurX -= bevelSpeed;
bevelFilter.blurY -= bevelSpeed;
}
/*如果我们确定模糊滤镜正被使用,我们让整个斜角不可见。(strength is 0).否则我们会看到一些难看的曲线。*/
if (bevelFilter.blurX == 0)
{
bevelFilter.strength = 0;
}
/*当我们改变了斜角滤镜时,我们需要重新分配一次滤镜*/
apple02.filters = [bevelFilter];
}
/*测试影片,用鼠标移入移出,观察效果。