Search code examples
actionscript-3displayobjectancestor

Easy way to get list of ancestor Classes for the object in Action Script 3?


I would like to check if object has a DisplayObject as one of it's ancestors and perform some operations on it if it has. Any quick and easy way to do this?


Solution

  • If by "ancestor" you mean "one of superclasses", then the solution is simple: in ActionScript an object can have "a DisplayObject as one of it's ancestors" only if object's class has DisplayObject in it's inheritance chain, which is easily checked by casting. Inheritance creates "IS A" relation between parent and child classes, so a child's instance IS An instance of parent (and of any other distant ancestor).

    var object:* = ....;
    if (object is DisplayObject) {
        var displayObject:DisplayObject = object as DisplayObject;
        // object has DisplayObject class in it's inheritance chain
        // do something with object using displayObject reference
    } 
    

    or

    var object:* = ....;
    var displayObject:DisplayObject = object as DisplayObject;
    if (displayObject != null) {
        // object has DisplayObject class in it's inheritance chain
        // do something with object using displayObject reference
    }