Search code examples
actionscript-3flashair

Cannot modify a MovieClip filter property


I applied a Bevel filter to a MovieClip. Later in my code, when I'm trying to change a filter property it doesn't seem to work.

Here is my code:

for (var i: int = 0; i < myMovieClip.filters.length; i++) {
    if (myMovieClip.filters[i] is BevelFilter) {
        trace("done");
        myMovieClip.filters[i].highlightColor = 0xFF0000;
        myMovieClip.filters[i].shadowColor = 0x0000FF;
        break;
    }
}

Solution

  • According to this guide from adobe: Graphic Effects Learning Guide for Flash: Filters

    Each filter has a set of properties unique to it. The filters can be accessed and modified just like a regular array object, although getting and setting the filters using the filters property returns a duplicate of the filters object instead of a reference.

    // This will not work
    my_mc.filters[0].blurX = 20;
    

    Because the previous code snippet returns a copy of the filters array, it modifies the duplicate. In order to modify the blurX property, you need to use the following ActionScript code instead:

    // This will work
    var filterList:Array = my_mc.filters;
    filterList[0].blurX = 20;
    my_mc.filters = filterList;
    

    So it seems that you need to change your code to something like this:

    var filtersList:Array = myMovieClip.filters;
    for (var i: int = 0; i < filtersList.length; i++) {
      if (filtersList[i] is BevelFilter) {
        trace("done");
        filtersList[i].highlightColor = 0xFF0000;
        filtersList[i].shadowColor = 0x0000FF;
        myMovieClip.filters = filtersList;
        break;
      }
    }