L6go1as
L6go1as

Reputation: 39

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);
    }
}

Upvotes: 0

Views: 147

Answers (3)

BotMaster
BotMaster

Reputation: 2223

The simple answer to your question is: no, a custom class cannot addChild without extending a subclass of DisplayObjectContainer. The method addChild() and its related methods are defined in DisplayObjectContainer and only subclasses of it can use them.

You often see the method addChild() used without a calling object (ex: theobject.addChild()) but that's only because the keyword "this" is implied. In reality addChild() is always called by a DisplayObjectContainer instance.

Upvotes: 0

Marty
Marty

Reputation: 39456

I looks like you're trying to do this:

public class MyClass
{
    private var sprite:Sprite;

    public function MyClass(container:MovieClip)
    //                      ^^^^^^^^^ Take a reference to a container to add children to.
    {
        sprite = new Sprite()
        sprite.graphics.lineStyle(1, 0x990000, 1);
        sprite.graphics.drawRoundRect(5, 5, 500, 150, 10, 10);

        container.addChild(sprite);
        // ^^^^^^ Add internal sprite to the referenced container.
    }
}

Where you provide a container to add children to.

Meaning your Main class will simply pass a reference to itself to your MyClass instance.

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

    var myVar:MyClass = new MyClass(this);
    //                              ^^^^ Simply pass the main class as the container.
}

Another option is to simply expose sprite from MyClass and then use:

addChild(myVar.sprite);

In your Main class.

Upvotes: 0

Timur Koshel
Timur Koshel

Reputation: 179

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

Upvotes: 4

Related Questions