开发者

ActionScript - Retrieving Index Of Specific Filter

i have a few filters on a sprite. on mouse over i would like to access one of the filters in the filters array, but i'm having a bit of trouble trying to accomplish this.

mySprite.filters = [new DropShadowFilter(), new GlowFilter(), new BlurFilter()];
mySprite.addEventListener(MouseEvent.MOUSE_OVER, mouseOverEventHandler);

function mouseOverEventHandler(evt:MouseEv开发者_运维技巧ent)
     {
     //obtain indexOf the GlowFilter
     trace(evt.currentTarget.filters[evt.currentTarget.filters.indexOf([Object GlowFilter])]));
     }

the above code doesn't work. what's the proper way to get the index of a specific filter in a filters array?


If I understand correctly, you're essentially trying to do this:

 var index:int = evt.currentTarget.filters.indexOf([Object GlowFilter]);

The bracketed part is not valid Actionscript it shouldn't even compile. What you need to do is to iterate over the filters and test them yourself since there's no way to search for a specific class with indexOf.

Try this instead:

function mouseOverEventHandler(evt:MouseEvent) {
    var glowFilter:GlowFilter;
    for (var i:int = 0; i < evt.target.filters.length; i++) {
        if (evt.target.filters[i] is GlowFilter) {
            glowFilter = evt.target.filters[i];
            break;
        }
    }
}

Also, if you're going to fiddle with the filters in the array Flash won't accept in-place modifications, so you need to re-set the array once you've changed it:

function mouseOverEventHandler(evt:MouseEvent) {
    var glowFilter:GlowFilter;
    for (var i:int = 0; i < evt.target.filters.length; i++) {
        if (evt.target.filters[i] is GlowFilter) {
            glowFilter = evt.target.filters[i];
            break;
        }
    }

    if (!glowFilter) return;

    glowFilter.blurX = 10;
    var filters:Array = evt.target.filters;
    filters[i] = glowFilter;
    evt.target.filters = filters;

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜