ActionScript Trace Array indexOf?
i have an filter array containing one drop shadow fil开发者_运维技巧ter. 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
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.
精彩评论