简体   繁体   English

在AS3中设置动画片段的宽度/高度

[英]Set width/height of a movieclip in AS3

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? 我有一个Player类,它是一个动画片段,我试图从中设置它的宽度和高度,然后将其添加到舞台上,但是由于某种原因,无论我做什么,this.width返回0并进行设置大小只适用于父类..为什么会发生这种情况?

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 谢谢

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. 我可能会将stage.addChild(this)移到Player之外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM