Set width/height of a movieclip in AS3

一世执手 提交于 2019-12-22 01:36:12

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/15164016/set-width-height-of-a-movieclip-in-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!