我有一些文字区域,通过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;
答案 0 :(得分:1)
没有任何功能。但是你可以遍历过滤器数组,找到过滤器并在找到它后得到它的颜色。
像这样(未经测试):
for(var i:int = 0; i < textfield1.filters.length;i++) {
if(textfield1.filters[i] is GlowFilter) {
textfield1.filters[i].color;
}
}
答案 1 :(得分:1)
这个问题已经接受了答案,但在此之前,我正在开设一个课程,这样可以使特定的BitmapFilter
更容易,哦,好吧,无论如何我都会发布我的答案。
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, 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
<强> [UPDATE] 强>
此更新是为了回应Juan Pablo Califano的第一条评论。 “将最后一行更改为return new BitmapFilterClass();
”的问题是它会返回BitmapFilter
的新实例,当我们想要的是返回已经存在的BitmapFilter
实例的引用时显示对象的filters
属性的数组。
例如,我们创建一个Sprite
对象,并将GlowFilter
的新实例添加到其filters
属性中,将颜色参数设置为0xFFFF00
(黄色):
var sprite:Sprite = new Sprite();
sprite.graphics.beginFill(0xFF0000);
sprite.graphics.drawCircle(0, 0, 100);
sprite.graphics.endFill();
sprite.filters = [new GlowFilter(0xFFFF00)];
sprite.x = stage.stageWidth / 2;
sprite.y = stage.stageHeight / 2;
addChild(sprite);
现在,当我们使用公共静态color
方法跟踪返回的GlowFilter
对象的getGlowFilter
属性时,我们得到以下内容:
trace(Filters.getGlowFilter(sprite).color); // output: 16776960(0xFFFF00, yellow);
但是,如果我们像建议的那样修改代码,我们会得到以下内容:
trace(Filters.getGlowFilter(sprite).color); // output: 16711680(0xFF0000, red);
现在,一种避免错误访问空值的简单方法是使用一个contional语句:
if (Filters.getGlowFilter(sprite) != null)
{
var spriteGlowFilter:GlowFilter = Filters.getGlowFilter(sprite);
spriteGlowFilter.blurX = 10;
spriteGlowFilter.blurY = 10;
sprite.filters = [spriteGlowFilter];
}// end if
[更新2]
我已更新了Filters
类,以便在使用BitmapFilter
等公共静态方法时,如果无法找到getBevelFilter()
,则会引发错误的简单错误处理功能。通过将相应的true或false布尔值设置为公共静态属性throwsError
,可以打开和关闭此功能。
var sprite:Sprite = new Sprite();
sprite.graphics.beginFill(0xFF0000);
sprite.graphics.drawCircle(0, 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, 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);