【问题标题】:Set width/height of a movieclip in AS3在 AS3 中设置动画剪辑的宽度/高度
【发布时间】:2013-03-01 18:00:53
【问题描述】:

我有这个 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);

    }

}

谢谢

【问题讨论】:

    标签: actionscript-3


    【解决方案1】:

    尝试以下方法:

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

    您应该在将显示对象添加到舞台后修改它们的宽度/高度。我可能也会将 stage.addChild(this) 移到 Player 之外。

    【讨论】:

    • 我以前打过这个,而 MC 或其他 DisplayObject 无法访问舞台,它的宽度和高度是假的。添加到阶段解决了这个问题。很好的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-18
    • 2014-06-15
    • 1970-01-01
    • 1970-01-01
    • 2015-04-29
    • 2012-10-02
    相关资源
    最近更新 更多