views:

71

answers:

1

i have an filter array containing one drop shadow filter. trace is returning -1 instead of 0 for indexOf after it is tracing that the array contains the object. please explain.

trace(filterObject);
trace(displayObject.filters);
trace(displayObject.filters.indexOf(filterObject));

//outputs:
//
// [object DropShadowFilter]
// [object DropShadowFilter]
// -1
+1  A: 

Looks like filters are copied behind the scenes when you apply them. That is, the filter stored in the filters array is not the same object you passed. Since indexOf compares object reference, you get -1, indicating the object you passed to the method is not contained in the array.

This little snippet shows this more clearly:

var filter:DropShadowFilter = new DropShadowFilter();
var sprite:Sprite = new Sprite();
sprite.filters = [filter];

trace(sprite.filters[0] == filter); // false!

It's worth noting that every BitmapFilter has a clone() method, which I assume is being called internally to make a fresh copy of the object.

Juan Pablo Califano
humm... so is there no way to reliably track the index of a filter in the filters array?
TheDarkInI1978
Other than using a loop and checking the type of the filter, I don't think so.
Juan Pablo Califano
how do you check the type of a filter? use toString()? or is there a more appropriate way?
TheDarkInI1978
I think using the `is` operator is a better approach. `if(filter is DropShadowFilter ) { // code here }`.
Juan Pablo Califano