Search code examples
actionscript-3classextending

Why can't extend DisplayObject then instantiate


The DisplayObject class can be extended but the extended class cannot be instantiated.

Class:

package{

    import flash.display.DisplayObject;

    class Extended extends DisplayObject{

        public function Extended(){
            super()
        }
    }
}

Main timeline:

var e:Extended=new Extended();

addChild(e);

Error:

ArgumentError: Error #2012: Extended$ class cannot be instantiated.


Solution

  • Please read the documentation of the DisplayObject class:

    The DisplayObject class itself does not include any APIs for rendering content onscreen. For that reason, if you want create a custom subclass of the DisplayObject class, you will want to extend one of its subclasses that do have APIs for rendering content onscreen, such as the Shape, Sprite, Bitmap, SimpleButton, TextField, or MovieClip class.