In a sane world, this works as expected:
var array:Array = ['a','b','c'];
trace(array.indexOf(array[0])); // returns 0
In an insane world, this happens:
trace(Screen.screens.indexOf(Screen.screens[0])); // returns -1
… if Screen.screens is an Array of the available instances of Screen, why can’t that array give an accurate indexOf one of its own children?
edit – To take it a step further, check this out:
for each(var i:Screen in Screen.screens){
for each(var j:Screen in Screen.getScreensForRectangle(this.stage.nativeWindow.bounds)){
trace(i, j, i == j); // returns false
trace(i.bounds, j.bounds, i.bounds == j.bounds); // returns false
}
}
At least one Screen listed in Screen.screens should be identical to a Screen in Screen.getScreensForRectangle(this.stage.nativeWindow.bounds) – but even if you compare the Screen.bounds, it still doesn’t match up, despite the two Rectangle objects having the same dimensions!
Insanity, ladies and gentlemen! You don’t even want to see the workaround I put together (hint: it involves comparing the values of Screen.bounds.toString() for the contents of Screen.screens)
This is an educated(?) guess, but since
Screen.screensis read only, and modifying the array it returns has no effect, I think it’s a fairly safe bet that internally, every time you call it Flash generates and returns a new array ofScreenobjects (rather than keeping an internal set ofScreenobjects and giving you access to them). When you call:you make two separate accesses to
Screen.screens, so if each of those calls is returning a different array of objects, it’s easy to see why you don’t find any matches – because theindexOfmethod tests for === equality, so two differentScreenobjects won’t match, even if they happen to contain information about the same physical screen.The solution is to grab a copy of the screens array and use it. This works fine: