问题描述
|
我有一些通过AS3添加了辉光的文本字段,例如下面的代码。
var glow:GlowFilter = new GlowFilter();
glow.color = 0x00FFFF;
glow.alpha = 1;
glow.blurX = 5;
glow.blurY = 5;
glow.quality = BitmapFilterQuality.HIGH;
textfield1.filters = [glow];
我想知道是否有检索发光颜色的函数!
像这样:
textfield2.getGlowFilter.color;
解决方法
没有任何功能。但是,您可以遍历滤镜数组,查找滤镜,并在找到滤镜后获得其颜色。
像这样(未经测试):
for(var i:int = 0; i < textfield1.filters.length;i++) {
if(textfield1.filters[i] is GlowFilter) {
textfield1.filters[i].color;
}
}
,这个问题的答案已经被接受,但是在此之前,我正在上一堂课,可以使特定的3英镑变得更容易,哦,好吧,我还是会发布我的答案。
Filters.as:
package filters
{
import flash.display.DisplayObject;
import flash.filters.BevelFilter
import flash.filters.BitmapFilter;
import flash.filters.BlurFilter
import flash.filters.ColorMatrixFilter
import flash.filters.ConvolutionFilter
import flash.filters.DisplacementMapFilter
import flash.filters.DropShadowFilter
import flash.filters.GlowFilter
import flash.filters.GradientBevelFilter
import flash.filters.GradientGlowFilter
public class Filters
{
public static var throwsError:Boolean;
public static function getBevelFilter(displayObject:DisplayObject):BevelFilter
{
return BevelFilter(getBitmapFilter(displayObject,BevelFilter));
}// end function
public static function getBlurFilter(displayObject:DisplayObject):BlurFilter
{
return BlurFilter(getBitmapFilter(displayObject,BlurFilter));
}// end function
public static function getColorMatrixFilter(displayObject:DisplayObject):ColorMatrixFilter
{
return ColorMatrixFilter(getBitmapFilter(displayObject,ColorMatrixFilter));
}// end function
public static function getConvolutionFilter(displayObject:DisplayObject):ConvolutionFilter
{
return ConvolutionFilter(getBitmapFilter(displayObject,ConvolutionFilter));
}// end function
public static function getDisplacementMapFilter(displayObject:DisplayObject):DisplacementMapFilter
{
return DisplacementMapFilter(getBitmapFilter(displayObject,DisplacementMapFilter));
}// end function
public static function getDropShadowFilter(displayObject:DisplayObject):DropShadowFilter
{
return DropShadowFilter(getBitmapFilter(displayObject,DropShadowFilter));
}// end function
public static function getGlowFilter(displayObject:DisplayObject):GlowFilter
{
return GlowFilter(getBitmapFilter(displayObject,GlowFilter));
}// end function
public static function getGradientBevelFilter(displayObject:DisplayObject):GradientBevelFilter
{
return GradientBevelFilter(getBitmapFilter(displayObject,GradientBevelFilter));
}// end function
public static function getGradientGlowFilter(displayObject:DisplayObject):GradientGlowFilter
{
return GradientGlowFilter(getBitmapFilter(displayObject,GradientGlowFilter));
}// end function
public static function updateFilters(displayObject:DisplayObject,...bitmapFilters:Array):void
{
for (var i:uint = 0; i < bitmapFilters.length; i++)
{
if (!(bitmapFilters[i] is BitmapFilter))
{
throw new ArgumentError(\"bitmapFilters argument must be of type BitmapFilter\");
}// end if
}// end for
var filterArray:Array = new Array();
for each(var bitmapFilter:BitmapFilter in displayObject.filters)
{
filterArray.push(bitmapFilter);
}// end for
for (var j:uint = 0; j < filterArray.length; j++)
{
for (var k:uint = 0; k < bitmapFilters.length; k++)
{
if (filterArray[j] is Class(Object(bitmapFilters[k]).constructor))
{
filterArray[j] = bitmapFilters[k];
}
else
{
if (throwsError) throw new Error(\"One or more bitmapFilters arguments did not exist in the display object\'s filters array\");
}// end else
}// end for
}// end for
displayObject.filters = filterArray;
private static function getBitmapFilter(displayObject:DisplayObject,BitmapFilterClass:Class):BitmapFilter
{
var bitmapFilter:BitmapFilter;
for each(var bf:BitmapFilter in displayObject.filters)
{
if (bf is BitmapFilterClass)
{
bitmapFilter = bf;
break;
}// end if
}// end for
if (!bitmapFilter && throwsError)
throw new Error(\"Could not find BitmapFilter\");
return bitmapFilter;
}// end function
}// end class
}// end package
Main.as:
package
{
import filters.Filters;
import flash.display.Sprite;
import flash.events.Event;
import flash.filters.GlowFilter;
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE,init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE,init);
var sprite:Sprite = new Sprite();
sprite.graphics.beginFill(0xFF0000);
sprite.graphics.drawCircle(0,100);
sprite.graphics.endFill();
sprite.filters = [new GlowFilter()];
sprite.x = stage.stageWidth / 2;
sprite.y = stage.stageHeight / 2;
addChild(sprite);
var spriteGlowFilter:GlowFilter = Filters.getGlowFilter(sprite);
spriteGlowFilter.blurX = 10;
spriteGlowFilter.blurY = 10;
sprite.filters = [spriteGlowFilter];
}// end function
}// end class
}//end package
[更新]
此更新是对Juan Pablo Califano的第一条评论的回应。 \“将最后一行更改为return new BitmapFilterClass();
\”的问题在于,当我们要返回显示对象的filters
属性中已经存在的BitmapFilter
实例的引用时,它将返回BitmapFilter
的新实例。的数组。
例如,我们创建一个Sprite
对象,并向其filters
属性添加GlowFilter
的新实例,并将color参数设置为0xFFFF00
(黄色):
var sprite:Sprite = new Sprite();
sprite.graphics.beginFill(0xFF0000);
sprite.graphics.drawCircle(0,100);
sprite.graphics.endFill();
sprite.filters = [new GlowFilter(0xFFFF00)];
sprite.x = stage.stageWidth / 2;
sprite.y = stage.stageHeight / 2;
addChild(sprite);
现在,当我们使用公共静态getGlowFilter
方法跟踪返回的GlowFilter
对象的color
属性时,将得到以下信息:
trace(Filters.getGlowFilter(sprite).color); // output: 16776960(0xFFFF00,yellow);
但是,如果我们像建议的那样更改代码,则会得到以下结果:
trace(Filters.getGlowFilter(sprite).color); // output: 16711680(0xFF0000,red);
现在,避免访问空值时出错的一种更简单的方法是使用条件语句:
if (Filters.getGlowFilter(sprite) != null)
{
var spriteGlowFilter:GlowFilter = Filters.getGlowFilter(sprite);
spriteGlowFilter.blurX = 10;
spriteGlowFilter.blurY = 10;
sprite.filters = [spriteGlowFilter];
}// end if
[更新2]
我已经更新了Filters
类,以合并一个简单的错误处理功能,如果使用诸如ѭ23之类的公共静态方法时找不到BitmapFilter
,则会抛出一个错误。通过为公共静态属性“ 24”设置相应的布尔值,可以打开和关闭此功能。
var sprite:Sprite = new Sprite();
sprite.graphics.beginFill(0xFF0000);
sprite.graphics.drawCircle(0,100);
sprite.graphics.endFill();
sprite.filters = [new GlowFilter(0xFFFF00)];
sprite.x = stage.stageWidth / 2;
sprite.y = stage.stageHeight / 2;
addChild(sprite);
Filters.throwsError = true;
Filters.getDropShadowFilter(sprite); // error: Could not find BitmapFilter
[更新3]
为了回应Juan Pablo Califano的第二条评论,我添加了公共静态方法updateFilters()
,这很容易解释,因此我将简单地展示其用法示例:
var sprite:Sprite = new Sprite();
sprite.graphics.beginFill(0xFF0000);
sprite.graphics.drawCircle(0,100);
sprite.graphics.endFill();
sprite.filters = [new GlowFilter()];
sprite.x = stage.stageWidth / 2;
sprite.y = stage.stageHeight / 2;
addChild(sprite);
Filters.throwsError = true;
var glowFilter:GlowFilter = Filters.getGlowFilter(sprite);
glowFilter.blurX = glowFilter.blurY += 10;
Filters.updateFilters(sprite,glowFilter);