开发者

How to get the glow color of a text in AS3?

开发者 https://www.devze.com 2023-02-26 18:34 出处:网络
I have some textfields with glow added thru AS3 like the code bellow. var glow:GlowFilter = new GlowFilter();

I have some textfields with glow added thru AS3 like the code bellow.

    var glow:GlowFilter = new GlowFilter();
    glow.color = 0x00FFFF;
    glow.alpha = 1;
    glow.blurX = 5;
    glow.blurY = 5;
    glow.quality = BitmapFilterQuality.HIGH;

    textfield1.filters = [glow];

I'd like to know if is there a function to retrieve the glow color!

Something like this:

tex开发者_JAVA技巧tfield2.getGlowFilter.color;


There's no function for that. But you could loop through the filters array, look for the filter and get its color once you find it.

Something like this (untested):

for(var i:int = 0; i < textfield1.filters.length;i++) {
    if(textfield1.filters[i] is GlowFilter) {
        textfield1.filters[i].color;
    }
}


An answer has already been accepted for this question but before it was, I was working on a class that would make getting a specific BitmapFilter easier, oh well, I'll post my answer anyway.

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]

This update is in response to Juan Pablo Califano's first comment. The problem with "Changing the last line to return new BitmapFilterClass();" is that it would return a new instance of BitmapFilter, when what we want is to return the reference of the BitmapFilter instance thats already in the display object's filters property's array.

For example we create a Sprite object and add a new instance of GlowFilter to its filters property, setting the color argument to 0xFFFF00(yellow):

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);

Now when we trace the color property of the returned GlowFilter object using the public static getGlowFilter method we get the following:

trace(Filters.getGlowFilter(sprite).color); // output: 16776960(0xFFFF00, yellow);

However if we altered the code like suggested we would get the following:

trace(Filters.getGlowFilter(sprite).color); // output: 16711680(0xFF0000, red);

Now a simpler way to avoid getting an error from accessing a null value is to use a contional statement:

if (Filters.getGlowFilter(sprite) != null)
{
    var spriteGlowFilter:GlowFilter = Filters.getGlowFilter(sprite);
    spriteGlowFilter.blurX = 10;
    spriteGlowFilter.blurY = 10;
    sprite.filters = [spriteGlowFilter];

}// end if

[UPDATE 2]

I've updated the Filters class to incorparate a simple error handling feature that throws an error if a BitmapFilter can't be found when using a public static method like getBevelFilter(). This feature can be toggled on and off by setting a respective true or false boolean value to the public static property 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

[UPDATE 3]

In response to Juan Pablo Califano's second comment, I've added the public static method updateFilters() which is pretty self explanatory so I'll simple show an example of its use:

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消