PDA

View Full Version : Looking for "is exactly"



m90
June 14th, 2009, 03:11 PM
Hello!

I have a problem. If I say "obj is Object" it will always return true as everything extends Object. Can I somehow find out if an object is exactly an instance of the specified class?

In my case I have both Sprites and MovieClips in the display list. Now I'd like to loop through the list and get the Sprites only. But if I say "obj is Sprite" this is of course true for the MovieClips as well!

Of course I can do:


if (obj is Sprite && !(obj is MovieClip)){
}


but I was wondering how to tackle this if the class you're looking for is "way up the ladder" and has lots of subclasses?

Any tip?

Thanks!

Krilnon
June 14th, 2009, 03:25 PM
The most reliable way is probably getDefinitionByName(getQualifiedClassName(someObje ct)). You can also use describeType, but it's probably a bit heavy for what you're trying to use and you probably don't need the extra info that it provides.

You can also use the constructor property, but it isn't completely reliable since it's a read-write property:

var o:Object = {};
o.constructor = Sprite;
trace(o.constructor == Object); // false
trace(getDefinitionByName(getQualifiedClassName(o) ) == Object); // true

m90
June 14th, 2009, 03:35 PM
Great! I think I'll stick with the getDefinitionByName as it does exactly what I was looking for!

Thanks!