Set width/height of a movieclip in AS3

actionscript-3

I have this Player class which is a movieclip,
I'm trying to set it's width and height from it and then make it add itself to the stage,
But for some reason, no matter what I do, this.width returns 0 and setting the size only works from the parent class.. why is this happening?

public class Player extends MovieClip {

    public var head:MovieClip = new Head_normal_front();

    public function Player(stage:Stage){
        this.addChild(head);
        this.width = 10;
        this.height = 10;
        stage.addChild(this);

    }

}

Thanks

Best Answer

Try something like the following :

public class Player extends MovieClip {

    public var head:MovieClip = new Head_normal_front();

    public function Player(stage:Stage){
        this.addChild(head);
        this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        stage.addChild(this);
    }

    private function onAddedToStage(e:Event):void
    {
        this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        this.width = 10;
        this.height = 10;
    }
}

You should modify the width/height of display objects after they are added to the stage. I would probably move the stage.addChild(this) outside of Player too.

Related Topic