Flash/AIR AS3: comparing contents of Screen.screens
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 Ar开发者_StackOverflow中文版ray
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.screens
is 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 of Screen
objects (rather than keeping an internal set of Screen
objects and giving you access to them). When you call:
Screen.screens.indexOf(Screen.screens[0])
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 the indexOf
method tests for === equality, so two different Screen
objects 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:
var scr:Array = Screen.screens;
trace( scr.indexOf(scr[0]) ); // returns 0
精彩评论