Search code examples
classactionscript-3oopflashflashdevelop

How to use addChild() w/o extension from another class?


Is it possible to use addChild() without 'extends' from another Class ? It's strange, that i need to extension from another classes to use it ... but maybe its my lack of knowledge in as3 ...

Main:

public class Main extends Sprite
{
    private var sprite:Sprite = new Sprite();

    public function Main()
    {
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        // entry point          

        var myVar:MyClass = new Myclass();
        addChild(myVar);
    }
}

MyClass:

public class MyClass
{
    private var sprite:Sprite = new Sprite();

    public function MyClass()
    {
        sprite.graphics.lineStyle(1, 0x990000, 1);
        sprite.graphics.drawRoundRect(5, 5, 500, 150, 10, 10);

        addChild(sprite);
    }
}

Solution

  • addChild is method that add's DisplayObject to DisplayObjectContainer, so yes, you must extend your custom classes if you want to see it on screen

    futher reading: http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7e3e.html http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html