0

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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.